1 technology stack
egg+node
Project Online Address
http://47.100.30.67:7001
Project git address
https://github.com/jiqingpeng/mars/tree/master/fe/egg-example
2 Quick Start
egg officially recommends that scaffolding be used directly and that projects can be generated quickly with a few simple instructions (npm > = 6.1.0):
mkdir egg-example && cd egg-example npm init egg --type=simple npm i
Start the project:
npm run dev open http://localhost:7001
3 Item Directory Structure
- app/router.js is used to configure URL routing rules, see Router .
- app/controller/** is used to parse user input and return the corresponding results after processing. See also Controller.
- The built-in object in the app/model/** framework can also be understood as the name of the interface. See also Model.
- Configg/config.default.js is used to write configuration files. See Specific To configure.
- config/plugin.js is used to configure the plug-ins that need to be loaded, see Specifically Plug-in unit.
Detailed analysis of 4 projects
Code parsing under app/router.js
'use strict'; /** * @param {Egg.Application} app - egg application */ module.exports = app => { const { router, controller } = app; router.get('/', controller.home.render);//home table interface router.resources('users', '/users', controller.users);//User table interface router.resources('info', '/info', controller.info);//User table interface router.resources('test', '/test', controller.test);//test table interface router.resources('phone', '/phone', controller.phone);//phone table interface };
app/controller/info.js
// app/controller/users.js const Controller = require ('egg').Controller; // Turn integer method function toInt (str) { if (typeof str === 'number') return str; if (!str) return str; return parseInt (str, 10) || 0; } //Filtering empty attributes in objects function filterJson (data) { const json = {}; for (item in data) { if (data[item]) { json[item] = data[item]; } } return json; } class InfoController extends Controller { // async index () { const ctx = this.ctx; let {offset, limit, sex, nick} = ctx.query; const where = filterJson ({sex, nick}); offset = toInt (offset); limit = toInt (limit); let total = null; let result = null; if (JSON.stringify (where) === '{}') { total = (await this.app.model.Info.findAll ({})).length; result = await this.app.model.Info.findAll ({ offset, limit, include: [ { model: this.app.model.Phone } ] }); } else { total = (await this.app.model.Info.findAll ({ where: where, })).length; result = await this.app.model.Info.findAll ({ offset, limit, where: where, include: [ { model: this.app.model.Phone } ] }); } ctx.body = { data: result, total, }; ctx.status = 201; } async show () { const ctx = this.ctx; const {id} = this.ctx.params; const Info = await ctx.model.Info.findById (id, { include: [ { model: this.app.model.Phone, }, ], }); ctx.body = { status: true, res: Info, }; ctx.status = 201; } async create () { const ctx = this.ctx; const {phone_id, nick, sex, head_url} = ctx.request.body; const info = await ctx.model.Info.create ({phone_id, nick, sex, head_url}); ctx.status = 201; ctx.body = { state: true, res: info, }; } async update () { const ctx = this.ctx; const id = toInt (ctx.params.id); const result = await ctx.model.Info.findById (id); const {nick, sex, head_url} = ctx.request.body; await result.update ({nick, sex, head_url}); ctx.status = 201; ctx.body = { state: true, res: result, }; } async destroy (id) { const ctx = this.ctx; await ctx.delete (id); ctx.status = 200; } } module.exports = InfoController;
- findByID/findAll and other methods as well as the use of query rules such as limt,offset,where are based on promise's ode.js ORM library Sequelize. See also Sequelize Docs Chinese Document Edition .
- The index/update/show/create/destroy restful style defines the interface rules as follows
See details. https://eggjs.org/zh-cn/basics/router.html .
5 Interface Document
phone
Request url:
- http://47.100.30.67:7001/info
Request method:
- GET
Parameters:
Parameter name | Is it necessary to fill in | type | Explain |
---|---|---|---|
phone | no | string | Cell-phone number |
Note that http get request parameters should be placed in the URL unless special processing is done
Request url:
- http://47.100.30.67:7001/info
Request method:
- POST
Parameters:
Parameter name | Is it necessary to fill in | type | Explain |
---|---|---|---|
phone | yes | string | Cell-phone number |
pwd | yes | string | Password |
Request url:
- http://47.100.30.67:7001/info
Request method:
- PUT
Parameters:
Parameter name | Is it necessary to fill in | type | Explain |
---|---|---|---|
pwd | no | string | Password |
Request url:
- http://47.100.30.67:7001/info
Request method:
- DELETE
Parameters:
Parameter name | Is it necessary to fill in | type | Explain |
---|---|---|---|
id | yes | int | Account id |