JavaScript Answers

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

Spread the love

To get path from the request with Node.js, we get the URL from the req object.

For instance, we write

const api = express.Router();

api.use((req, res, next) => {
  const fullUrl = req.protocol + "://" + req.get("host") + req.originalUrl;

app.use("/", api);

to get the request URL’s protocol with req.protocol

We get the request URL’s host name with req.get("host").

And we get the rest of the request URL with req.originalUrl.

By John Au-Yeung

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

Leave a Reply

Your email address will not be published.