This can produce different results when calling directly in base module i.e. main file (e.g. index.js
or app.js
) vs calling from inside module via app.use()
middleware i.e. route file (e.g. routes/users.js
).
API call:
http://localhost:8000/api/users/profile/123/summary?view=grid&leng=en
We'll be comparing our outputs against above API call
1) First, we'll see the result from inside module:
We'll be placing our user module inside routes directory, with one route
routes/users.js
const router = (require('express')).Router();
router.get('/profile/:id/:details', (req, res) => {
console.log(req.protocol); // http or https
console.log(req.hostname); // only hostname (abc.com, localhost, etc)
console.log(req.headers.host); // hostname with port number (if any)
console.log(req.header('host')); // <same as above>
console.log(req.route.path); // exact defined route
console.log(req.baseUrl); // base path or group prefix
console.log(req.path); // relative path except path
console.log(req.url); // relative path with query|search params
console.log(req.originalUrl); // baseURL + url
// Full URL
console.log(`${req.protocol}://${req.header('host')}${req.originalUrl}`);
res.sendStatus(200);
});
module.exports = router;
index.js
const app = (require('express'))();
const users = require('./routes/users');
app.use('/api/users', users);
const server = require('http').createServer(app);
server.listen(8000, () => console.log('server listening'));
Output
http
....................................................................................... [protocol]
localhost
.............................................................................. [hostname]
localhost:8000
..................................................................... [headers.host]
localhost:8000
..................................................................... [header('host')]
/profile/:id/:details
........................................................ [route.path]
/api/users
............................................................................. [baseUrl]
/profile/123/summary
.......................................................... [path]
/profile/123/summary?view=grid&leng=en
........................ [url]
/api/users/profile/123/summary?view=grid&leng=en
..... [originalUrl]
Full URL:
http://localhost:8000/api/users/profile/123/summary?view=grid&leng=en
2) Now, directly from main module:
We'll define our route right in the starting file (i.e. app.js or index.js)
index.js
const app = (require('express'))();
app.get('/api/users/profile/:id/:details', (req, res) => {
console.log(req.protocol); // http or https
console.log(req.hostname); // only hostname (abc.com, localhost, etc)
console.log(req.headers.host); // hostname with port number (if any)
console.log(req.header('host')); // <same as above>
console.log(req.route.path); // exact defined route
console.log(req.baseUrl); // base path or group prefix
console.log(req.path); // relative path except path
console.log(req.url); // relative path with query|search params
console.log(req.originalUrl); // baseURL + url
// Full URL
console.log(`${req.protocol}://${req.header('host')}${req.originalUrl}`);
res.sendStatus(200);
});
const server = require('http').createServer(app);
server.listen(8000, () => console.log('server listening'));
Output
http
........................................................................ [protocol]
localhost
............................................................... [hostname]
localhost:8000
...................................................... [headers.host]
localhost:8000
...................................................... [header('host')]
/profile/:id/:details
......................................... [route.path]
.............................................................................. [baseUrl]
/profile/123/summary
........................................... [path]
/profile/123/summary?view=grid&leng=en
......... [url]
/profile/123/summary?view=grid&leng=en
......... [originalUrl]
Full URL:
http://localhost:8000/api/users/profile/123/summary?view=grid&leng=en
We can clearly see in above output that the only difference is of baseUrl
which is empty string here. So, the originalUrl
also changes & looks same as the url
TypeError: Cannot read property 'path' of undefined
req.route
is undefined. Im using express 3.4.4. What can cause route to be undefined?