我是expressjs的新手,所以,我正在开发一个app,我需要在url中输入用户名。我不知道如何添加它。 所以我在路由中有2个文件,其中包含以下代码
exports.user = function (req, res) {
sess = req.session;
if (sess.cocoToken != undefined || sess.cocoToken != null) {
client.get(baseUrl + "/feed/", {headers: {"Authorization": "Token " + sess.cocoToken}}, function (data3, response3) {
if (response3.statusCode == 200) {
req.session.items = data3;
client.get(baseUrl + "/api/advice/", {headers: {"Authorization": "Token " + sess.cocoToken}}, function (data4, response4) {
if (response4.statusCode == 200) {
req.session.items = data4;
res.render('user', {
title: req.session.profile.username,
classname: 'feed',
profile: req.session.profile,
feedIitems: req.session.items,
advices: req.session.items
});
}
});
}
});
console.log(req.session.profile);
} else {
res.redirect("/logout")
}
};
在app.js中我调用路径
app.get('/user', routes.user);
现在如何将用户名放在网址中?
答案 0 :(得分:0)
你可以通过
来完成req.param('tagId')
答案 1 :(得分:0)
您可以将用户名作为查询参数传递,因为这是一个GET请求,例如http://example.com/user?username=user2991920。
在您的路线中,您可以使用req.query.username
exports.user = function(req, res) {
// req.query.username will have query username
}
您无需修改app.get('/user', routes.user);
答案 2 :(得分:0)
app.get('/user/:id', function (req, res) {
sess = req.session;
if (sess.cocoToken != undefined || sess.cocoToken != null) {
client.get(baseUrl + "/api/user/" + req.params.id + "/", {headers: {"Authorization": "Token " + sess.cocoToken}}, function (user, response3) {
if (response4.statusCode == 200) {
req.session.items = advices;
res.render('user', {
title: user.profile.username,
classname: 'feed',
user: user
});
}
});
}
});