Express: base url

Created on 7 May 2013  路  11Comments  路  Source: expressjs/express

How can I change the run of my app under a base url like

http://myserver:3000/base_url/

where base_url is relative for all the app?

Most helpful comment

Yes, express 4.x router makes it easy to mount 'mini-apps' on base-urls.

e.g. if you want to define a base url as /foo and then mount a bunch of routes /bar, /bar/:barId onto it so that the full paths would be /foo/bar, /foo/bar/:barId respectively, you will do the following:

var express = require('express');
var app = express();
var router = express.Router();

// this will only be invoked if the path ends in /bar
router
.route('/bar')
.all(function(req, res, next) {
  // middleware logic here for all verbs (get, put, post, delete)
  next();
})
.get(function(req, res, next) { 
  // invoked on get /foo/bar
  res.send('Hello world!');
});

router
.route('/bar/:barId')
.put(function(req, res, next) {
  // invoked on put /foo/bar/:barId
  res.send(req.params.barId);
});

// mount the '/bar' router on to the '/foo' base url
app.use('/foo', router);

app.listen(3000);

Modified example from : http://expressjs.com/4x/api.html#router

All 11 comments

not sure if i understand but:

var app = express();
var server = express();
server.use('/base_url', app);
http.Server(server).listen(3000);

that should work.

it works, but the public files like stylesheets, js,... at public directory don't work correctly I should change te templates for /base_url/js, /base_url/stylesheets

you should .use() the .static() middleware on the server, also this is not an issue with express, so you should try asking question about _how_ on stackoverflow, #express irc or the mailing list.

@visionmedia this can be closed :smile_cat:

The above suggestion (.use()'ing one express app inside another) works pretty much as expected - applies it to routes and static asset paths w/o any additional mucking-around as expected. But this does feel rather hack-ish.

The need to set a base or root URL is a rather common situation and topic (in relationship to express) has been discussed in a few places a long time ago (like here and here, and even the existence of this).

So with Express 4.x on the horizon, has there been a built-in way to do this cleanly?

Yes, express 4.x router makes it easy to mount 'mini-apps' on base-urls.

e.g. if you want to define a base url as /foo and then mount a bunch of routes /bar, /bar/:barId onto it so that the full paths would be /foo/bar, /foo/bar/:barId respectively, you will do the following:

var express = require('express');
var app = express();
var router = express.Router();

// this will only be invoked if the path ends in /bar
router
.route('/bar')
.all(function(req, res, next) {
  // middleware logic here for all verbs (get, put, post, delete)
  next();
})
.get(function(req, res, next) { 
  // invoked on get /foo/bar
  res.send('Hello world!');
});

router
.route('/bar/:barId')
.put(function(req, res, next) {
  // invoked on put /foo/bar/:barId
  res.send(req.params.barId);
});

// mount the '/bar' router on to the '/foo' base url
app.use('/foo', router);

app.listen(3000);

Modified example from : http://expressjs.com/4x/api.html#router

Would doing so also apply to the base URL to the .static middleware or just to those routes defined in the router object?

@morficus No, this would only affect routes starting with /foo as shown in app.use('/foo', router);. You can define a separate mount path for your static files.

Hi everyone,

I write because I am struggling with a similar problem.

I have a configured proxy reverse like this:

ProxyPass /web/ http://localhost:3000/web/

and I am using:
app.use('/web', router); to mount my nodejs app in '/web'.

However, each time that I try to execute my nodejs app, only html webpages are visualising, there is nor js neither css files because the browser try to get them from the root path '/'. Also, the routes in the views are not correct, because when I click on it they redirect me to the root '/' instead of '/web/'.
I do not know what I am doing wrong, I am considering to edit all the views and routes in order to include '/web/' in the path, but I think there has to be a better solution than this, can anybody help me?

Thank you very much in advance.

@MiguelAngelRG if you are using a proxy, use it for serving the static assets - js, css, images etc. Then you won't have to add /web to their path.

It's recommended to let a front facing proxy handle requests to static assets for Node.js apps.

@MiguelAngelRG this is why web resources should reference resources with relative urls whenever possible, not absolute urls!

Was this page helpful?
0 / 5 - 0 ratings

Related issues

ZeddYu picture ZeddYu  路  3Comments

dmaks9 picture dmaks9  路  3Comments

HafidAbnaou picture HafidAbnaou  路  3Comments

despairblue picture despairblue  路  3Comments

cuni0716 picture cuni0716  路  3Comments