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;
next();
});
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
.