Is it possible to set a base URL for NodeJS app? Is it possible to set a base URL for NodeJS app? express express

Is it possible to set a base URL for NodeJS app?


The express router can handle this since 4.0

http://expressjs.com/en/api.html#router

http://bulkan-evcimen.com/using_express_router_instead_of_express_namespace.html

var express = require('express');var app = express();var router = express.Router();// simple logger for this router's requests// all requests to this router will first hit this middlewarerouter.use(function(req, res, next) {  console.log('%s %s %s', req.method, req.url, req.path);  next();});// this will only be invoked if the path ends in /barrouter.use('/bar', function(req, res, next) {  // ... maybe some additional /bar logging ...  next();});// always invokedrouter.use(function(req, res, next) {  res.send('Hello World');});app.use('/foo', router);app.listen(3000);

Previous answer (before express 4.0) :

The express-namespace module (dead now) used to do the trick :

https://github.com/visionmedia/express-namespace

require('express-namespace');app.namespace('/myapp', function() {        app.get('/', function (req, res) {           // can be accessed from something.com/myapp        });});


At the moment this is not supported, and it's not easy to add it on your own.

The whole routing stuff is buried deep inside the server code, and as a bonus there's no exposure of the routes them selfs.

I dug through the source and also checked out the latest version of Express and the Connect middleware, but there's still no support for such functionality, you should open a issue either on Connect or Express itself.

Meanwhile...

Patch the thing yourself, here's a quick and easy way with only one line of code changed.

In ~/.local/lib/node/.npm/express/1.0.0/package/lib/express/servers.js, search for:

// Generate the routethis.routes[method](path, fn);

This should be around line 357, replace that with:

// Generate the routethis.routes[method](((self.settings.base || '') + path), fn);

Now just add the setting:

app.set('base', '/myapp');

This works fine with paths that are plain strings, for RegEx support you will have to hack around in the router middleware yourself, better file an issue in that case.

As far as the static provider goes, just add in /mypapp when setting it up.

Update

Made it work with RegExp too:

// replacethis.routes[method](baseRoute(self.settings.base || '', path), fn);// helperfunction baseRoute(base, path) {    if (path instanceof RegExp) {        var exp = RegExp(path).toString().slice(1, -1);        return new RegExp(exp[0] === '^' ? '^' + base + exp.substring(1) : base + exp);    } else {        return (base || '') + path;    }}

I only tested this with a handful of expressions, so this isn't 100% tested but in theory it should work.

Update 2

Filed an issue with the patch:
https://github.com/visionmedia/express/issues/issue/478


Just to update the thread, now with Express.js v4 you can do it without using express-namespace:

var express = require('express'),    forumRouter = express.Router(),    threadRouter = express.Router(),    app = express();forumRouter.get('/:id)', function(req, res){  res.send('GET forum ' + req.params.id);});forumRouter.get('/:id/edit', function(req, res){  res.send('GET forum ' + req.params.id + ' edit page');});forumRouter.delete('/:id', function(req, res){  res.send('DELETE forum ' + req.params.id);});app.use('/forum', forumRouter);threadRouter.get('/:id/thread/:tid', function(req, res){  res.send('GET forum ' + req.params.id + ' thread ' + req.params.tid);});forumRouter.use('/', threadRouter);app.listen(app.get("port") || 3000);

Cheers!