【问题标题】:app.get doesn't work properly express.jsapp.get 无法正常工作 express.js
【发布时间】:2012-12-25 11:16:06
【问题描述】:
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 不渲染,但在其他不以 / 结尾的页面中它可以工作。

【问题讨论】:

    标签: javascript node.js redirect express


    【解决方案1】:

    不肯定我理解这个问题,但你应该能够通过改变来让它工作

    app.get('/:id', 函数(req, res){

    app.get('/:id/', 函数(req, res){

    png 应该渲染 如果它不起作用,请告诉我

    【讨论】:

    • 我都试过了,但有人工作。使用app.get('/:id', function (req, res){,当我尝试访问localhost:8888/randomuser 时,突然,我被重定向到localhost:8888/randomuser/,我不知道为什么!我认为这就是问题所在。
    【解决方案2】:

    您将被重定向到 /randomuser/ 或 /randomuser,具体取决于 app.get 中的路由。 据我所知,这没关系。 如果您还有其他问题,请告诉我

    【讨论】:

    • 这是应该做的,但是,如果我使用app.get('/:id', function (req, res){app.get('/:id/', function (req, res){,结果是一样的,localhost:8888/randomuser/,当我想要localhost:8888/randomuser,但app.get('/:id', function (req, res){ 没有不适合这个。
    • 我有一个新的疑问,为什么当我在localhost:8888/user/randomuser,甚至在localhost:8888/randomuser/(因为我不知道为什么我不能去localhost:8888/randomuser)时,我可以' t 从静态目录渲染图像。例如,如果我写localhost:8888/user/randomuser 这个<img src="<%= avatar %>">,图像不会出现。而<%= avatar %>./randomuser/avatar.png
    猜你喜欢
    • 1970-01-01
    • 2016-12-01
    • 1970-01-01
    • 2016-09-01
    • 2012-07-11
    • 2018-04-08
    • 2017-04-20
    • 2018-10-02
    • 2016-09-04
    相关资源
    最近更新 更多