Saturday, 27 May 2017

How do I redirect relative to the express application root?

I want to make my app so that it can be mounted at some location other than /. When I read the docs for express-4.x’s res.redirect(), it says I have three options:

  1. Absolute URI (http://example.org/path/nested)
  2. Host-relative (/path/nested)
  3. URI-relative (nested)

I don’t see any option for application-relative redirects.

Example of how an app might be mounted within another:

'use strict';

const express = require('express');

const app = express();
module.exports = app;

const subApp = express();
app.use('/subApp', subApp);

subApp.get('/blah/*', (req, res) => {
    if (/dee$/.test(req.url)) {
        res.end('end');
    } else {
        res.redirect('dee');
    }
});

How do I redirect to the root URI of subApp without requiring the code in subApp to know its mountpoint? Maybe there is a way I can construct it from information in req?



via binki

No comments:

Post a Comment