expressjsHow do I get the URL in Express.js?
Getting the URL in Express.js is a straightforward process. To do so, you can use the request object that is passed to each route handler.
The request object has a url property that contains the requested URL. The following example demonstrates how to get the URL in Express.js.
app.get('/', (req, res) => {
const url = req.url;
console.log(url);
res.send('Hello World');
});
The output of the above code block would be the requested URL.
The code consists of:
app.get: This is the Express.js method to create a route handler for aGETrequest.req.url: This is therequestobject'surlproperty that contains the requested URL.console.log: This is used to log the requested URL to the console.res.send: This is used to send a response back to the client.
Helpful links
More of Expressjs
- How do I use Yarn to add Express.js to my project?
- How do I find Express.js tutorials on YouTube?
- How do I use Express.js to parse YAML files?
- How can I set up X-Frame-Options in ExpressJS?
- How can I disable the X-Powered-By header in Express.js?
- How do I use Express.js and Yarn together in a software development project?
- How do I implement CSRF protection in an Express.js application?
- What is Express.js and how is it used for software development?
- How do I create a tutorial using Express.js?
- How can I use express-zip js to zip and download files?
See more codes...