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

Add "set" Method to "Router" #2524

Open
akesser opened this issue Feb 2, 2015 · 6 comments
Open

Add "set" Method to "Router" #2524

akesser opened this issue Feb 2, 2015 · 6 comments

Comments

@akesser
Copy link

akesser commented Feb 2, 2015

Hey guys,
I think it would be nice to be able to set options to single Routes. For example one can set a default layout for the handlebars rendering engine with

app.set('view options', { layout: 'main' });

or with

app.engine(".hbs", handlebars({
    defaultLayout: "main", 
}));

When using a express.Router() for example with app.use("/admin", adminRouter) it would be nice to be able to use something like

adminRouter.set('view options', { layout: 'adminMain' });

to prevent using

res.render('home', {layout: "adminMain"});

for every call to res.render

Thanks

-André-

@akesser
Copy link
Author

akesser commented Feb 2, 2015

I found that one way to solve my special Layout problem is

adminRouter.use(function(req, res, next)
{
    res.locals.layout = "adminMain";
    next();
});

@akesser
Copy link
Author

akesser commented Feb 3, 2015

The solution from yesterday is no real solution.

Consider the following example

var adminRouter = express.Router();
adminRouter.use(function(req, res, next)
{
    res.locals.layout = "adminMain";
    next();
});
adminRouter.get("/", admin.admin);

app.use("/admin", adminRouter);
app.get("/register", register.register);

When I first visit the /registerroute, everything is fine.
But when I call /admin/and then the /register route, res.locals.layout is set to "adminMain". As a result to this, in the /register route the wrong layout is rendered.

@dougwilson
Copy link
Contributor

This needs discussion, but from me I am (currently) -1, because that's what the difference is between an app and a router: an app has settings.

@grabbou
Copy link

grabbou commented May 21, 2015

In case you need different settings, you should go with a sub-app pattern and in your main express app, actually use it.

firstExpressapp.use('/admin/', anotherExpressApp);

@dougwilson is there any performance impact when using sub-apps instead of nested Router? I think there shouldn't be as app delegates all the requests to the top-level router anyway.

@snovak7
Copy link

snovak7 commented Jan 16, 2016

Well from scoping perspective... in version 3 this was great, because there was no Router, with addition to router, app -> router -> response; router should/could get a locals attribute that would upgrade locals to the response object and possible views that would use them. You can do this manually as @akesser pointed out.

@Fishrock123
Copy link
Contributor

Also with @dougwilson on this -- the functionality is probably better suited in it's own sub-module somehow.

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

No branches or pull requests

5 participants