简体   繁体   English

节点js表达虽然使用app.param 4 url​​ get请求不起作用

[英]node js express Whil using app.param 4 url get request doesn't work

I am using app.param to enter a parameter into the url which is the user username and it works except that any get requests right after that do not load the html page for the client. 我正在使用app.param向用户名url中输入一个参数,它的工作原理是,之后的任何获取请求都不会为客户端加载html页面。 I am not getting any type of error except after the page fails to load i get a 404 error on the page. 除了页面加载失败后,我没有任何类型的错误,页面上出现404错误。 What could be the issue, i even tried router.param instead and got the same result. 可能是什么问题,我什至尝试使用router.param并得到了相同的结果。 obviously the functionallity requires this to work on multiple pages. 显然,功能性要求此功能可在多个页面上工作。

//Profile views for other users

app.param('username', function(req, res, next, username) {

    User.findOne({ 'local.username' : username }, function(err, user) {

        if(err)
            throw err;

        if(user==null) 
            return false;

        req.person = user;

    next();

    });

});

app.get('/:username', function(req, res) {

    Profile.findOne({ 'pic.username' : req.person.local.username }, function(err, profilepic) {

        if(err) {
            throw err;
        }
        console.log(profilepic);

        res.render('pages/visitprofile', {
        user : req.person,
        profilepic : profilepic

    });
    });

});

app.get('/photos/:username', function(req, res) {

    Profile.findOne({ 'pic.username' : req.person.local.username }, function(err, profilepic) {

        if(err)
            throw err;

        res.render('pages/photos', {
            profilepic : profilepic
        });
    });
});

app.listen(3000);
console.log('express is listening on port 3000');

When you are using app.get('/:username', callback) syntax, you need to look for req.params.username . 使用app.get('/:username', callback)语法时,需要查找req.params.username

app.get('/photos/:username', function(req, res) {
  res.send(req.params.username);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM