express get url path code example
Example 1: express get full url
var fullUrl = req.protocol + '://' + req.get('host') + req.originalUrl;
Example 2: express get raw path
const url = require('url');
const express = require('express');
const app = express();
app.use((req, res, next) => {
const path = url.parse(req.url).path;
});
const port = 3000;
app.listen(port, console.log(`Listening on port ${port}.`));
Example 3: get full url nodejs
var fullUrl = req.protocol + '://' + req.get('host') + req.originalUrl;
Example 4: express request path
app.get('/admin', (req, res, next) => {
req.originalUrl;
req.baseUrl;
req.path;
req.baseUrl + req.path;
});
Example 5: express route parameters
Route path: /users/:userId/books/:bookId
Request URL: http://localhost:3000/users/34/books/8989
req.params: { "userId": "34", "bookId": "8989" }
Example 6: express route parameters
Route path: /flights/:from-:to
Request URL: http://localhost:3000/flights/LAX-SFO
req.params: { "from": "LAX", "to": "SFO" }