egg的resful风格

Method Path Route Name Controller.Action
GET /posts posts app.controllers.posts.index
GET /posts/new new_post app.controllers.posts.new
GET /posts/:id post app.controllers.posts.show
GET /posts/:id/edit edit_post app.controllers.posts.edit
POST /posts posts app.controllers.posts.create
PUT /posts/:id post app.controllers.posts.update
DELETE /posts/:id post app.controllers.posts.destroy

以vue + axios的技术基础做比较

业务层

  1. <script>
  2. import * as HomeAction from '@/api/home'
  3. export default {
  4. name: 'Home',
  5. created () {
  6. },
  7. methods: {
  8. fetchModel () {
  9. HomeAction.fetchModel({ method: 'get' }).then(res => {
  10. console.log(res)
  11. }).catch(err => console.error(err))
  12. },
  13. update () {
  14. HomeAction.update({ id: 15 }).then(res => {
  15. console.log(res)
  16. }).catch(err => console.error(err))
  17. },
  18. deleteModel () {
  19. HomeAction.destroy(15).then(res => {
  20. console.log(res)
  21. }).catch(err => console.error(err))
  22. },
  23. fetchList () {
  24. HomeAction.fetchList({ method: 'post' }).then(res => {
  25. console.log(res)
  26. }).catch(err => console.error(err))
  27. }
  28. }
  29. }
  30. </script>

API层

  1. import fly from '@/utils/axios'
  2. // 获取用户信息
  3. export function fetchModel (params) {
  4. return fly.get('/test/show', { params })
  5. }
  6. // 增加
  7. export function fetchList (params) {
  8. return fly.post('/test', params)
  9. }
  10. // 修改
  11. export function update (params) {
  12. return fly.put('/test/' + params.id, params)
  13. }
  14. // 删除
  15. export function destroy (id) {
  16. return fly.delete('/test/' + id)
  17. }

以Egg + mongodb为后端技术架构为基础比较

业务层

  1. 'use strict';
  2. const Controller = require('egg').Controller;
  3. class TestController extends Controller {
  4. constructor(ctx) {
  5. super(ctx);
  6. this.indexRule = {
  7. page: { type: 'int?' },
  8. page_size: { type: 'int?' },
  9. };
  10. }
  11. async index() {
  12. const { ctx } = this;
  13. ctx.body = 'test resful 接口风格测试';
  14. }
  15. // 增加
  16. async create() {
  17. const { ctx } = this;
  18. const res = ctx.request.body;
  19. ctx.body = { status: 200, data: res, msg: '增加' };
  20. }
  21. // 改
  22. async update() {
  23. const { ctx } = this;
  24. const res = ctx.params;
  25. ctx.body = { status: 200, data: res, msg: '改' };
  26. }
  27. // 查
  28. async show() {
  29. const { ctx } = this;
  30. const res = ctx.query;
  31. ctx.body = { status: 200, data: res, msg: '查' };
  32. }
  33. // 删
  34. async destroy() {
  35. const { ctx } = this;
  36. const res = ctx.params;
  37. console.log(res, 2);
  38. ctx.body = { status: 200, data: res, msg: '删' };
  39. }
  40. }
  41. module.exports = TestController;

接口定义

  1. 'use strict';
  2. /**
  3. * @param {Egg.Application} app - egg application
  4. */
  5. module.exports = app => {
  6. const { router, controller } = app;
  7. router.get('/', '/api/', controller.home.index);
  8. router.resources('test', '/api/test', controller.test);
  9. router.resources('post', '/api/post', controller.post);
  10. };