Appearance
Express.mountpath
Express.mountpath属性是子程序挂载的路径模式。
TIP
一个子程序是一个express的实例,其可以被用来作为路由句柄来处理请求
Express
// 导入模块 express
const express = require('express')
const app = express() // the main app
const admin = express() // the sub app
admin.get('/', (req, res) => {
console.log(admin.mountpath) // => /admin
res.send('Admin Homepage')
})
app.use('/admin', admin) // 挂载在 app实例上
它和req对象的req.baseUrl属性比较相似,除了req.baseUrl是匹配的URL路径,而不是匹配的模式。如果一个子程序被挂载在多条路径模式,app.mountpath就是一个关于挂载路径模式项的列表,如下面例子所示。
Express
var admin = express();
admin.get('/', function (req, res) {
console.log(admin.mountpath); // [ '/adm*n', '/manager' ]
res.send('Admin Homepage');
});
var secret = express();
secret.get('/', function (req, res) {
console.log(secret.mountpath); // /secr*t
res.send('Admin Secret');
});
admin.use('/secr*t', secret); // load the 'secret' router on '/secr*t', on the 'admin' sub app
app.use(['/adm*n', '/manager'], admin); // load the 'admin' router on '/adm*n' and '/manager', on the parent app