Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

[Feature Request]: Allow for setting a base path for routes #478

Closed
BonsaiDen opened this Issue · 13 comments

6 participants

@BonsaiDen

People apparently want that:

http://stackoverflow.com/questions/4375554/is-it-possible-to-set-a-base-url-for-nodejs-app/

All it needs is a simple patch to server.js.

When generating the route modify:

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

To this:

// Generate the route
this.routes[method](baseRoute(self.settings.base || '', path), fn);

And the helper for patching the routes:

function 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;
    }
}

One can now do:

var app = express.createServer();
app.set('base', '/myapp');
app.get('/test', function(req, res) {
    res.send('Hello World');
});
app.listen(4000);

And http://localhost:4000/myapp/test will server Hello World.

I've tested the RegExp patching with a couple of custom expressions as well with the ones from the API Docs.

@tj
tj commented

hmm pretty hackish, I will think about it :p it would be nice to tackle this issue at the core and not just express support, since this does not fix connect middleware like staticProvider

@BonsaiDen

Yeah it certainly would, but then again, giving people a .diff with 200 lines changed... dude I just want ma repz!

Just kidding, it took me like 30 minutes to come up with the thing above. I just wasn't sure how to patch the connect middleware without breaking everything else for him which uses it. If you can tell me a good way to hook this in, well I got plenty of free time ;)

@tj
tj commented

yeah tough call I will have to think about it a little.

@jwcooper

This would be a pretty useful feature. Right now I have references to the basePath all over the place, although, I'm sure there is a better way of doing it (like the above patch).

@tj
tj commented

we could create a connect middlware for this, quite easily actually, similar to the vhost one

@dizlexik

I believe 'express-contrib/namespace' does what you're trying to do:
https://github.com/visionmedia/express-contrib

@tj
tj commented

closing

@martintajur

Sorry for bringing this ancient topic up, but was there a solution to this?

The express-contrib URL given above does not work, and I don't see this kind of functionality in ExpressJS documentation.

@martintajur

OK, the correct URL is https://github.com/visionmedia/express-namespace and it works. Thanks.

@vvo

I thought I could use the basepath setting for this kind of actions. I was wrong maybe other people will be.

http://expressjs.com/guide.html#settings

Why not using the basepath setting for handling this?

Thank you.

@tj
tj commented

it shouldn't really be a setting IMO, the implementation with regexps is a hack. It's a lot easier to just use the "mounting" feature of Connect which makes that prefix/basepath transparent to the rest of the application

@vvo

The only issue I wanted to raise was that the basepath current param is misleading because we could think that it will act as a basepath routing param.

I have a case where I want to serve /basepath/url when NODE_ENV is production and /url when in dev mode.

And I really thought the basepath param was doing that. Should I use app mounting? Can't find a good example from the guide.

Thank you.

@tj
tj commented

@vvo definitely, I agree. I've removed it in 3.x for that reason. there are a few ways you can do it, mounting being one but I'll make sure to add those in the new guide im writing

@morficus morficus referenced this issue
Closed

base url #1611

This issue was closed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.