Home  >  Q&A  >  body text

express - 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,路由,页面都由前台完成。

黄舟黄舟2715 days ago796

reply all(2)I'll reply

  • 黄舟

    黄舟2017-04-17 14:57:37

    Yes, POST, DELETE, PUT, and GET correspond to addition, deletion, modification, and query respectively.

    reply
    0
  • 怪我咯

    怪我咯2017-04-17 14:57:37

    Personal understanding:
    For example, if you want to operate test. Then, the route should look like this /api/test. The front desk can use get, post, put, delete, etc. to access this route to complete the business. It should no longer be api/testGET or api/testPost.

    reply
    0
  • Cancelreply