简体   繁体   中英

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. I am not getting any type of error except after the page fails to load i get a 404 error on the page. What could be the issue, i even tried router.param instead and got the same result. 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('/photos/:username', function(req, res) {
  res.send(req.params.username);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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