Deepak Kumar
Deepak Kumar

Reputation: 101

How to get current url path in express with ejs

I want to search browser a query in Nodejs for How can I get current page URL Express with Ejs

Upvotes: 9

Views: 28513

Answers (2)

DanJHill
DanJHill

Reputation: 177

Bhaurao's answer is good - but to give Deepak his full request (make the path available in EJS), you can do the following:

In your application code where you initialise Express:

let app=express();
app.use ((req, res, next) => {
    res.locals.url = req.originalUrl;
    res.locals.host = req.get('host');
    res.locals.protocol = req.protocol;
    next();
});

Now, in your EJS, you should have access to the variables url, host and protocol. For example: <%= url %> will print the URL.

Hope that helps.

Upvotes: 7

Bhaurao Birajdar
Bhaurao Birajdar

Reputation: 1507

The protocol is available as req.protocol. docs here

var fullUrl = req.protocol + '://' + req.get('host') + req.originalUrl;

By using above example, you can get full page URL.

Upvotes: 15

Related Questions