Yalamber
Yalamber

Reputation: 7580

How to get current url in view in expressjs

I want to change class of some menu items based on the current url being viewed by user in expressjs app. I am using ejs templating system in expressjs How do i pass current url and mangage current view class in links? Should I pass current url vale in each res.render? or is some variable passed in view that can be used?

Upvotes: 2

Views: 3255

Answers (2)

Yalamber
Yalamber

Reputation: 7580

We can actually use

//pass user data
app.use(function(req, res, next) {
res.locals.variable = req.variable;
next();
});

and access the variable in view file.

Upvotes: 5

Gal Ben-Haim
Gal Ben-Haim

Reputation: 17803

you can pass the actual class or an indicator for which class to use as a template variable in res.render instead of passing the url and doing the actual parsing within the template.

Upvotes: 0

Related Questions