Express router.route:404错误

时间:2016-12-06 16:18:38

标签: javascript node.js express

第一条路线工作正常,但是当我发送第二条路线的请求时,我得到404。

http://localhost:3000/api/posts/发送GET请求返回:

  {
    message: "TODO return all posts"
  }

但是向http://localhost:3000/api/posts/1234发送GET请求会返回:

404
Error: Not Found

我错过了什么吗?

var express = require('express');
var router = express.Router();


router.route('/posts')
  //returns all posts
  .get(function(req, res) {
    res.send({message: 'TODO return all posts'});
  })

  .post(function(req, res) {
      res.send({message: 'TODO Create a new post'});
  });

router.route('/posts/:id')
  //returns a particular post
  .get(function(req, res) {
    res.send({message: 'TODO return post with ID ' + req.params.id})
  })

  //update existing post
  .put(function(req, res) {
    res.send({message: 'TODO modify post with ID ' + req.params.id})
  })

  //delete existing post
  .delete(function(req, res) {
    res.send({message: 'TODO delete post with ID ' + req.params.id})
  });

module.exports = router;

1 个答案:

答案 0 :(得分:1)

不确定这是否是拼写错误,但网址不是localhost:3000/posts/1234,而不是localhost:3000/index/posts/1234