1.直接从主文件(app.js / index.js)调用:
app.get('/admin/:foo/:bar/:baz', async (req, res) => {
console.log(req.originalUrl);
console.log(req.url);
console.log(req.path);
console.log(req.route.path);
console.log(req.baseUrl);
console.log(req.hostname);
console.log(req.headers.host) // OR req.header('host'));
console.log(req.protocol);
res.sendStatus(200);
});
接口调用:
http://localhost:3000/admin/a/b/c
输出
/admin/a/b/c
(originalUrl)
/admin/a/b/c
(url)
/admin/a/b/c
(path)
/admin/:foo/:bar/:baz
(route.path) (baseUrl)
(
localhost
hostname)
localhost:3000
(headers.host (hostname with port number))
http
(protocol)
2.从模块调用:
应用程序.js
const express = require('express');
const app = express();
...
const users = require('./users');
app.use('/api/users', users);
用户.js
const express = require('express');
const router = express.Router();
...
router.get('/admin/:foo/:bar/:baz', async (req, res) => {
console.log(req.originalUrl);
console.log(req.url);
console.log(req.path);
console.log(req.route.path);
console.log(req.baseUrl);
console.log(req.hostname);
console.log(req.headers.host) // OR req.header('host'));
console.log(req.protocol);
res.sendStatus(200);
});
接口调用:
http://localhost:3000/admin/a/b/c
输出
/api/users/admin/a/b/c
(originalUrl)
/admin/a/b/c
(url)
/admin/a/b/c
(path)
/admin/:foo/:bar/:baz
(route.path) (baseUrl)
/api/users
(
localhost
hostname)
localhost:3000
(headers.host (hostname with port number))
http
(protocol)