Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
BonsaiDen opened this issue Dec 7, 2010 · 13 comments
Closed

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

BonsaiDen opened this issue Dec 7, 2010 · 13 comments

Comments

@BonsaiDen
Copy link

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
Copy link
Member

tj commented Dec 7, 2010

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
Copy link
Author

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
Copy link
Member

tj commented Dec 9, 2010

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

@jwcooper
Copy link

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
Copy link
Member

tj commented Jan 24, 2011

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

@dizlexik
Copy link

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

@tj
Copy link
Member

tj commented Mar 2, 2011

closing

@martintajur
Copy link

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
Copy link

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

@vvo
Copy link

vvo commented May 14, 2012

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
Copy link
Member

tj commented May 14, 2012

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
Copy link

vvo commented May 15, 2012

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
Copy link
Member

tj commented May 15, 2012

@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

This issue was closed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants