当在基本模块中直接调用时,这可能会产生不同的结果,例如主文件(如index.js
或app.js
)与通过app.use()
中间件从模块内部调用时(如路由文件routes/users.js
)。
API调用:
http://localhost:8000/api/users/profile/123/summary?view=grid&leng=en
我们将与上述API调用的输出进行比较。
首先,我们将看到来自内部模块的结果:
我们将把用户模块放在路由目录中,其中包含一个路由,即/profile/:id/:details
routes/users.js文件
const router = (require('express')).Router();
router.get('/profile/:id/:details', (req, res) => {
console.log(req.protocol);
console.log(req.hostname);
console.log(req.headers.host);
console.log(req.route.path);
console.log(req.baseUrl);
console.log(req.path);
console.log(req.url);
console.log(req.originalUrl);
console.log(`${req.protocol}://${req.header('host')}${req.originalUrl}`);
res.sendStatus(200);
});
module.exports = router;
现在,我们将在应用程序的主模块中导入此用户模块,并使用app.use()
中间件将/api/users
添加为用户模块的基本路径
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'));
输出
[req.protocol] ............. http
[req.hostname] .......... localhost
[req.headers.host] ..... localhost:8000
[req.route.path] .......... /profile/:id/:details
[req.baseUrl] .............. /api/users
[req.path] ................... /profile/123/summary
[req.url] ...................... /profile/123/summary?view=grid&leng=en
[req.originalUrl] .......... /api/users/profile/123/summary?view=grid&leng=en
完整的URL:
http://localhost:8000/api/users/profile/123/summary?view=grid&leng=en
现在,如果我们直接在主模块中添加路由,我们将看到结果:
我们将在
主模块(即app.js或index.js)中定义路由,并连接基本路径
/app/users
与路由路径,而不是使用中间件。因此,路由将变为
/api/users/profile/:id/:details
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 without port
console.log(req.headers.host); // hostname with port number (if any); same result with req.header('host')
console.log(req.route.path); // exact defined route
console.log(req.baseUrl); // base path or group prefix
console.log(req.path); // relative path except query params
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'));
```
输出
[req.protocol] ............. http
[req.hostname] .......... localhost
[req.headers.host] ..... localhost:8000
[req.route.path] .......... /api/users/profile/:id/:details
[req.baseUrl] ..............
[req.path] ................... /api/users/profile/123/summary
[req.url] ...................... /api/users/profile/123/summary?view=grid&leng=en
[req.originalUrl] .......... /api/users/profile/123/summary?view=grid&leng=en
完整的URL:
http://localhost:8000/api/users/profile/123/summary?view=grid&leng=en
我们可以清楚地看到上面的输出中唯一的区别是baseUrl
为空字符串。因此,originalUrl
也会改变,并且与url
相同。
req.route
未定义。我正在使用express 3.4.4。什么原因会导致路由未定义? - davidpfahler