I want to search browser a query in Nodejs for How can I get current page URL Express with Ejs
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With