How to Proxy Requests to Pa11y Webservice Hosted on Heroku

How to Proxy Requests to Pa11y Webservice Hosted on Heroku

Official Pa11y logo https://pa11y.org/resources/brand/logo.svg
Official Pa11y logo https://pa11y.org/resources/brand/logo.svg

Pa11y-Dashboard runs of top of Pa11y Webservice (WS).
By default Pa11y WS listens on port 3000 and Pa11y Dashboard listens on port 4000.
Unfortunately Heroku doesn’t allow its users to easily expose multiple ports per application.

To overcome this limitation, I’ve decided to make Pa11y-Dashboard proxy requests to WS via /ws/ endpoint.

After a bit of searching I’ve found a dedicated proxy module for expressjs called express-http-proxy .

Integration is really simple as it’s literally 2 lines of code:

@@ -23,12 +23,28 @@ const hbs = require('express-hbs');
 const http = require('http');
 const pkg = require('./package.json');
+
+// Initialize proxy module
+const proxy = require('express-http-proxy');
+
 module.exports = initApp;
 
        let webserviceUrl = config.webservice;
        if (typeof webserviceUrl === 'object') {
                webserviceUrl = `http://${webserviceUrl.host}:${webserviceUrl.port}/`;
@@ -54,6 +70,13 @@ function initApp(config, callback) {
                extended: true
        }));
+
+       // Proxy requests to WebService
+       // It will use the same basic auth credentials as all other endpoints
+       app.express.use('/ws/', proxy(webserviceUrl));

Once the change is deployed to Heroku you will be able to access Pa11y Webservice via /ws/ endpoint, e.g.:

curl --user a your-pa11y-app.herokuapp.com/ws/tasks?lastres=true

PS. If you followed my earlier tutorial on adding basic-auth to Pa11y , then you should know that basic-auth will be also added to /ws/ endpoint.