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 aGET
request.req.url
: This is therequest
object'surl
property 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 find Express.js tutorials on YouTube?
- How can I render HTML pages using Express.js?
- How can I set up the folder structure for an Express.js project?
- How can I use OpenTelemetry with Express.js?
- How do I use adm-zip with Express.js?
- How do I use Express.js to create a YouTube clone?
- How can I disable the X-Powered-By header in Express.js?
- How do I download a zip file using Express.js?
- How can I set up X-Frame-Options in ExpressJS?
- How can I set up unit testing for an Express.js application?
See more codes...