我正在使用express + node.js,并且我有一个req对象,浏览器中的请求是/ account,但是当我登录req.path时,我得到的是'/'---不是'/ account'。
//auth required or redirect
app.use('/account', function(req, res, next) {
console.log(req.path);
if ( !req.session.user ) {
res.redirect('/login?ref='+req.path);
} else {
next();
}
});
req.path是/何时应该是/ account?
自己玩了一些游戏之后,应该使用:
console.log(req.originalUrl)
在某些情况下,您应该使用:
req.path
这为您提供了路径,而不是完整的请求URL。例如,如果您只对用户请求的页面感兴趣,而对URL的所有参数都不感兴趣:
/myurl.htm?allkinds&ofparameters=true
req.path将为您提供:
/myurl.html
作为补充,这是从文档扩展而来的示例,该示例很好地包装了您需要在所有情况下使用express来访问路径/ URL的所有知识:
app.use('/admin', function (req, res, next) { // GET 'http://www.example.com/admin/new?a=b'
console.dir(req.originalUrl) // '/admin/new?a=b' (WARNING: beware query string)
console.dir(req.baseUrl) // '/admin'
console.dir(req.path) // '/new'
console.dir(req.baseUrl + req.path) // '/admin/new' (full path without query string)
next()
})
基于:https : //expressjs.com/en/api.html#req.originalUrl
结论:如上述c1moore的回答所述,请使用:
var fullPath = req.baseUrl + req.path;
它应该是:
req.url
快递3.1.x
//auth required or redirect
app.use('/account', function(req, res, next) {
console.log(req.path);
if ( !req.session.user ) {
res.redirect('/login?ref='+req.path);
} else {
next();
}
});
req.path是/何时应该是/ account?
原因是Express减去了处理程序函数的安装路径,'/account'
在这种情况下就是这样。
他们为什么这样做呢?
因为这样可以更轻松地重用处理程序函数。您可以使处理程序函数执行以下操作req.path === '/'
,req.path === '/goodbye'
例如:
function sendGreeting(req, res, next) {
res.send(req.path == '/goodbye' ? 'Farewell!' : 'Hello there!')
}
然后,您可以将其安装到多个端点:
app.use('/world', sendGreeting)
app.use('/aliens', sendGreeting)
给予:
/world ==> Hello there!
/world/goodbye ==> Farewell!
/aliens ==> Hello there!
/aliens/goodbye ==> Farewell!
如果您想只获取没有查询字符串的“路径”,则可以使用url
库来解析并仅获取URL的路径部分。
var url = require('url');
//auth required or redirect
app.use('/account', function(req, res, next) {
var path = url.parse(req.url).pathname;
if ( !req.session.user ) {
res.redirect('/login?ref='+path);
} else {
next();
}
});
对于4.x版,您现在可以使用req.baseUrl
除req.path
来获取完整路径。例如,OP现在将执行以下操作:
//auth required or redirect
app.use('/account', function(req, res, next) {
console.log(req.baseUrl + req.path); // => /account
if(!req.session.user) {
res.redirect('/login?ref=' + encodeURIComponent(req.baseUrl + req.path)); // => /login?ref=%2Faccount
} else {
next();
}
});
req.route.path为我工作
var pool = require('../db');
module.exports.get_plants = function(req, res) {
// to run a query we can acquire a client from the pool,
// run a query on the client, and then return the client to the pool
pool.connect(function(err, client, done) {
if (err) {
return console.error('error fetching client from pool', err);
}
client.query('SELECT * FROM plants', function(err, result) {
//call `done()` to release the client back to the pool
done();
if (err) {
return console.error('error running query', err);
}
console.log('A call to route: %s', req.route.path + '\nRequest type: ' + req.method.toLowerCase());
res.json(result);
});
});
};
执行后,我在控制台中看到以下内容,并且在浏览器中得到了完美的结果。
Express server listening on port 3000 in development mode
A call to route: /plants
Request type: get
本文地址:http://javascript.askforanswer.com/ruheshiyongexpress-reqduixianghuoquqingqiulujing.html
文章标签:express , javascript , node.js
版权声明:本文为原创文章,版权归 javascript 所有,欢迎分享本文,转载请保留出处!
文章标签:express , javascript , node.js
版权声明:本文为原创文章,版权归 javascript 所有,欢迎分享本文,转载请保留出处!
评论已关闭!