Categories
JavaScript Answers

How to get the path from the request with Node.js?

Spread the love

Sometimes, we want to get the path from the request with Node.js.

In this article, we’ll look at how to get the path from the request with Node.js.

How to get the path from the request with Node.js?

To get the path from the request with Node.js, we can use the req.url property.

For instance, we write

const http = require('http');
const url = require('url');

http
  .createServer((req, res) => {
    res.send(req.url)
  })
  .listen(8080, '0.0.0.0');

to call createServer with a callback that gets the request path from req.url.

And we call res.send to send that has the response.

Conclusion

To get the path from the request with Node.js, we can use the req.url property.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published. Required fields are marked *