2012-12-25 25 views
0
app.get('/:id', function (req, res){ 

    UserModel.find({ user: req.params.id}, function (err, user){ 

     if (err) throw err; 

     console.log(user + '\n\n'); 

     res.render('profile.ejs', { 

       username: user[0].user, 
       avatar: user[0].avatar, 
       followers: user[0].followers.length, 
       following: user[0].following.length 

     }) 

    }); 

}); 

我不知道为什么,当,例如,我写<a href="/<%= username %>">...</a>,我去/randomuser/代替/randomuser。这是一个问题,如果我尝试从一个静态目录呈现/:id中的PNG?因为我认为是的! PNG不渲染,但在其他页面,它不会在/结束它的工作。app.get不能正常工作express.js

回答

0

不积极我理解的问题,但你应该能够得到它通过改变

app.get(工作 '/:身份证',函数(REQ,RES){

app.get( '/:ID /',函数(REQ,RES){

png应该呈现 如果它的剂量工作让我知道

+0

我试了两个,但任何人工作。用'app.get('/:id',函数(req,res){',当我尝试访问'localhost:8888/randomuser'时,突然,我被重定向到'localhost:8888/randomuser/,我不知道为什么!我认为这是问题。 – MrMangado

0

您将获取重定向到/ randomuser /或/ randomuser,具体取决于app.get中的路由。据我所知,它并不重要。 让我知道如果您有任何其他问题

+0

这就是应该做的,但是,如果我使用'app.get('/:id',function(req,res){'或'app。当我想要'localhost:8888/randomuser'时,get('/:id /',function(req,res){',结果是一样的'localhost:8888/randomuser /','app.get '/:id',function(req,res){'不适用于此。 – MrMangado

+0

我有一个新的疑问,为什么当我在'localhost:8888/user/randomuser'中,甚至在'localhost:8888 /随机用户/'(因为我不知道为什么我不能去'localhost:8888/randomuser'),我无法从静态目录中渲染图像。例如,如果我在'localhost:8888/user/randomuser'这个' MrMangado