node.js 后台只提供restfulAPI,到底是个什么样的设计?

这是我前台发起的请求测试.

    this.$http
      .get('http://localhost:3000/api/testGet', {
        params: {
          name: 'get'
        }
      })
      .then(function (data) {
        this.Get = data.body.msg
      })

    this.$http
      .delete('http://localhost:3000/api/testDelete', {
        params: {
          name: 'delete'
        }
      })
      .then(function (data) {
        this.Delete = data.body.msg
      })

    this.$http
      .post('http://localhost:3000/api/testPost', {
        name: 'post'
      })
      .then(function (data) {
        this.Post = data.body.msg
      })

    this.$http
      .put('http://localhost:3000/api/testPut', {
        name: 'put'
      })
      .then(function (data) {
        this.Put = data.body.msg
      })

这是我后台的处理

router.get('/testGet', function (req, res, next) {
  res.send({msg: 'get '});
})

router.delete('/testDelete', function (req, res, next) {
  res.send({msg: 'delete '});
})

router.post('/testPost', function (req, res, next) {
  res.send({msg: 'post '});
})

router.put('/testPut', function(req, res, next) {
  res.send({msg: 'put '});
})

我不太明白,是否根据 函数的功能来决定这个方法是属于get,delete,put,post中的哪一种?目前 我后台express只提供api,路由,页面都由前台完成。

阅读 3.4k
2 个回答

是的POST,DELETE,PUT,GET分别对应增删改查。

个人理解:
比如要操作test。那么,路由就应该是类似这个样子/api/test,前台使用get,post,put,delete等即可访问这个路由完成业务。而不应该再是api/testGET或者api/testPost

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题