所以我有一个路由js文件来处理来自特定端点的所有请求。我需要从中间件中获取完整的URL而不是相对url但是它似乎没有工作。这是我的路线档案:
var express = require('express');
var passport = require('passport');
var router = express.Router();
var Member = require('../model/member');
var isAuthenticated = require('./middleware').isAuthenticated;
var responseData = {
title: 'Dita Admin - Members',
id: 'members',
heading: 'Members',
members: [],
errors: {},
create: true
};
/* GET members page. */
router.get('/', isAuthenticated, function (req, res, next) {
Member.find({}, function (err, members) {
if (err) {
console.log(err);
} else {
responseData.members = members;
}
res.render('members', responseData);
});
});
module.exports = router;
这是中间件:
var isAuthenticated = function (req, res, next) {
if (req.isAuthenticated()) {
return next();
}
req.session.returnTo = req.path;
console.log(req.session.returnTo);
res.redirect('/login');
};
module.exports = {
isAuthenticated: isAuthenticated
};
每当我尝试打印网址时,我都会收到/
。但是端点是/members
。
提前谢谢。
答案 0 :(得分:3)
请求对象上有一个baseUrl
属性,它为您提供了路由器实例的挂载路径。所以你可以使用:
例如在你的成员路由器中:
router.get('/', isAuthenticated, function (req, res, next) {
console.log(req.baseUrl); // /members
});