Coder Social home page Coder Social logo

arsenic's Introduction

Arsenic

  • structure

    Arsenic Project
    ├── config
    |   ├── config.default.js
    |   ├── config.daily.js
    |   └── config.online.js
    ├── controller
    |   └── user
    |      └── index.js
    ├── service
    |   └── user
    |      └── index.js
    ├── router
    |   └── user.js
    ├── lib
    ├── test
    ├── index.js
    └── package.json
    
  • router

    Router 主要用来描述请求 URL 和具体承担执行动作的 Controller 的对应关系

    // router/user.js
    const router = require("@yeliex/arsenic").router();
    
    router.mount('/user', ctx => ctx.controller.user );
      // ctx.get('/users', ctx => ctx.controller.user.list);
      // ctx.post('/users', ctx => ctx.controller.user.create);
      // ctx.get('/users/:id', ctx => ctx.controller.user.item);
      // ctx.put('/users/:id', ctx => ctx.controller.user.update);
      // ctx.delete('/users/:id', ctx => ctx.controller.user.destroy);
    
    router.get('/users',ctx => ctx.controller.user.list);
    
    router.post('/users',ctx => ctx.controller.user.list);
    
    module.exports = router;
    
  • controller

    Controller 接受用户的参数,从数据库中查找内容返回给用户或者将用户的请求更新到数据库中

    框架推荐 Controller 层主要对用户的请求参数进行处理(校验、转换),然后调用对应的 service 方法处理业务,得到业务结果后封装并返回

    • 获取用户通过 HTTP 传递过来的请求参数
    • 校验、组装参数
    • 调用 Service 进行业务处理,必要时处理转换 Service 的返回结果,让它适应用户的需求
    • 通过 HTTP 将结果响应给用户
    // controller/user/index.js
    const Context = require('@yeliex/arsenic').Context;
    
    class UserController extends Context {
      list() {
        return this.Service.user.getUserList();
      }
      create(){
        return this.Service.user.getUser({phone: this._POST.phone}).then((user)=>{
          if(user){
            return Promise.reject('user exist');
          }
          return this.Service.user.addUser(this._POST);
        });
      }
      destroy(){
        return this.Service.user.getUser({phone: this._POST.phone}).then((user)=>{
          if(user){
            return Promise.reject('user exist');
          }
          return this.Service.user.deleteUser({id: this._POST.id});
        });
      }
    }
    
    module.exports = UserController;
    
  • service

    Service 是在复杂业务场景下用于做业务逻辑封装的一个抽象层

    • 保持 Controller 中的逻辑更加简洁
    • 保持业务逻辑的独立性,抽象出来的 Service 可以被多个 Controller 重复调用
    • 将逻辑和展现分离,更容易编写测试用例
    • 复杂数据的处理
    • 第三方服务的调用
    // service/user/index.js
    const Context = require('@yeliex/arsenic').Context;
    
    class UserService extends Context {
      getUserList(){
        return this.db.userDB.findAll();
      }
    }
    
    // OR
    const {userDB} = require('../../libs/db');
    
    exports.getUserList = () => {
      return userDB.findAll();
    }
    

arsenic's People

Contributors

yeliex avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.