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

浏览:25日期:2022-10-07

问题描述

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

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

问题解答

回答1:

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

回答2:

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

相关文章: