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

how to add the functionality to express.Router() #13

Closed
RamyRais opened this issue Apr 8, 2016 · 5 comments
Closed

how to add the functionality to express.Router() #13

RamyRais opened this issue Apr 8, 2016 · 5 comments

Comments

@RamyRais
Copy link

RamyRais commented Apr 8, 2016

How can i add this functionality to express.Router() ?
this will not work even with removing the router.extendExpress and router.registerAppHelpers
var searchRouter = express.Router(); router.extendExpress(searchRouter); router.registerAppHelpers(searchRouter); searchRouter.post('something', 'something'

@alubbe
Copy link
Owner

alubbe commented Apr 9, 2016

Just to clarify, you are working with express.Router but not an express app? How does the above code fail?
If you could provide me with a slightly longer example, I will take a look at it.

@RamyRais
Copy link
Author

RamyRais commented Apr 9, 2016

so this is my use case:

var searchRouter = express.Router();
searchRouter.post('/doctors/:speciality/:governorate', 'search.doctor', function(){...});
//others routes
server.use('/search', searchRouter); //where server is my express app

So here i need to add router.extendExpress(searchRouter); which is logic but the problem is the route will miss the '/search'. you will get '/doctors/:speciality/:governorate' instead of '/search/doctors/:speciality/:governorate'.

PS: I figured out there is no need to call registerAppHelpers on the searchRouter.

@alubbe
Copy link
Owner

alubbe commented Apr 9, 2016

That behaviour is intended and correct. When you create instances of express.Router, those instances are designed to be isolated and not supposed to know where they are used (in this case, inside of /search). Compare this with how the req and res objects inside searchRouter also have no knowledge that they live inside of /search.
To show you why, imagine you embedded searchRouter more than once, like this:

server.use('/search', searchRouter);
server.use('/preciseSearch', searchRouter); 

If you then called build on 'search.doctor', what should it return? It should return just /doctors/:speciality/:governorate because it is isolated. Your application is the one orchestrating where it is being used and should prepend /search or /preciseSearch, whichever one is correct.

@RamyRais
Copy link
Author

RamyRais commented Apr 9, 2016

okay thank you

@agibralter
Copy link

I feel like it would be nice to have first class support for this... would there be any way to hook into server.use(prefix, router) such that the prefix became a part of the router.build method?

Could there be a method on the request with the context of where the current Router is mounted...? I don't know... just trying to wrap my head around this one. Seems like a shame that if you want to use Routers to isolate parts of an app that you have to go back to manually constructing URLs...

var express = require('express');
var app = express();

var Router = require('named-routes');
var router = new Router();
router.extendExpress(app);
router.registerAppHelpers(app);

app.get('/admin/user/:id', 'admin.user.edit', function(req, res, next){
    var url = req.namedRoutes.build('admin.user.edit', {id: 2}); // => /admin/user/2
});

var searchRouter = express.Router();
router.extendExpress(searchRouter);

searchRouter.post('/users', 'users', function(req) {
    var url = req.namedRoutes.build('users'); // => /search/users
});

app.use('/search', searchRouter);

app.listen(3000);

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

3 participants