nodeJS中无法获取/错误

时间:2017-04-23 06:58:17

标签: javascript angularjs node.js express

我正在开发一个简单的MEAN堆栈应用程序。我正在使用角度路由重定向到基于点击项目的ID的页面,如下所示:

   when('/story/:id', {
      template: '<news-detail></news-detail>'
   })

点击某个项目时,应该转到如下页面:http://localhost:3002/story/58fbcf765865db1d8da94b41

但是在那个页面上我收到了“Can not GET / story / 58fbcf765865db1d8da94b41”节点错误。

从后端我试过这个:

 app.get('/story/:id', function(req, res){
   if(req){
    var id = req.params.id;
    PostProfile.News.findById(id, function(err, item){
        if(item){
            console.log("found")
            res.send(item)
        }
    })
  }
})

此解决方案只在页面上显示一些原始json

 {"_id":"58fbc2834f675c1cdb7dc628","title": .....

这是什么左右,我怎样才能使用angular来通过id来发出请求而不首先通过nodejs。

非常感谢。

1 个答案:

答案 0 :(得分:0)

转到某个页面,你必须做res.render 例如:

    router.get('/story/:id', function(req, res) {
    var id = req.params.id;
    PostProfile.News.findById(id, function(err, item){
      res.render('somePage', {
        pageTitle: item.title,
        body: item.body
     });
   })
});
相关问题