express - node.js 后台只提供restfulAPI,到底是个什么样的设计?
黄舟
黄舟 2017-04-17 14:55:37
0
2
795

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

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

黄舟
黄舟

人生最曼妙的风景,竟是内心的淡定与从容!

reply all(2)
黄舟

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

刘奇

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.

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!