This is the fourth day of my participation in Gwen Challenge
I. Use scaffolding to build projects quickly
$ mkdir egg-example && cd egg-example
$ npm init egg --type=simple
$ npm i
Copy the code
Project directory
An egg - project ├ ─ ─ package. Json ├ ─ ─ app. Js (optional) ├ ─ ─ agent. The js (optional) ├ ─ ─ app (focus) | ├ ─ ─ the router. The js │ ├ ─ ─ controller │ | └ ─ ─ Home. Js │ ├ ─ ─ service (optional) │ | └ ─ ─ the user. The js │ ├ ─ ─ middleware (optional) │ | └ ─ ─ response_time. Js │ ├ ─ ─ the schedule (optional) │ | └ ─ ─ My_task. Js │ ├ ─ ─ public (optional) │ | └ ─ ─ reset. CSS │ ├ ─ ─ the view (optional) │ | └ ─ ─ home. TPL │ └ ─ ─ the extend (optional) │ ├ ─ ─ helper. Js │ (optional) ├ ─ ─ request. Js (optional) │ ├ ─ ─ the response. The js (optional) │ ├ ─ ─ the context, js (optional) │ ├ ─ ─ application. Js (optional) │ └ ─ ─ agent. The js (optional) ├ ─ ─ Config (focus) | ├ ─ ─ plugin. Js | ├ ─ ─ config. The default. The js │ ├ ─ ─ config. Prod. Js | ├ ─ ─ config. The test. The js (optional) | ├ ─ ─ Config. Local. Js (optional) | └ ─ ─ config. The unittest, js (optional) └ ─ ─ the test ├ ─ ─ middleware | └ ─ ─ response_time. Test. The js └ ─ ─ the controller └ ─ ─ home. Test. JsCopy the code
To build a successful
Start trying to create your first mock interface
1. Create a new list method in home.js
Async list () {this. CTX. Body = {MSG: 'ok', the list: [1, 2, 3, 4, 5]}}Copy the code
2. Register the route in route.js
router.get('/list', controller.home.list);
Copy the code
3. The operation is successful
Try to create a new route
1. Environment preparation, VScode install egg.js plug-in, easy to prompt code.
2. Create a route
egg controller
Copy the code
3. Create an API in the new user.js route
'use strict';
const Controller = require('egg').Controller;
class UserController extends Controller {
async list() {
let res = {
msg: 'ok',
data: [
{
id: '1',
username: '张三',
nickname: 'zs',
sex: '男'
},
{
id: '2',
username: '王五',
nickname: 'ww',
sex: '女'
},
]
}
const {ctx} = this
ctx.body = res
}
}
module.exports = UserController;
Copy the code
4. Configure the route in router.js
Succeeded in creating a route
5. Two parameter transmission modes of routes
1. Params (restful Style)
'use strict'; const Controller = require('egg').Controller; Const userList = [{id: '1', username: 'nickname ', nickname: 'zs', nickname:' nickname ', {id: '2', username: 'nickname ', nickname: 'zs', nickname:' nickname ', const userList = [{id: '1', username: 'nickname ', nickname: 'zs', nickname:' nickname ', {id: '2', username: 'nickname ', nickname: },] class UserController extends Controller {async List () {const {CTX} = this let res = {MSG: 'ok', data: userList } ctx.body = res } async info1() { const { ctx } = this let userDetail = userList.find(v => v.id === ctx.params.id) let res = { msg: 'ok', data: userDetail } ctx.body = res } } module.exports = UserController;Copy the code
Running effect
2. Query
async info2() {
const { ctx } = this
let userDetail = userList.find(v => v.id === ctx.query.id)
let res = {
msg: 'ok',
data: userDetail
}
ctx.body = res
}
Copy the code
router.get('/user/info2', controller.user.info2)
Copy the code
6. Turn off CSRF security
Create a new POST request, access will be denied using postman tests
router.post('/user/create', controller.user.create)
Copy the code
The solution
1. Install cross-domain plug-ins
npm i egg-cors --save
Copy the code
2. Configure the config/plugin. Js
cors: {
enable: true,
package: 'egg-cors'
}
Copy the code
3. Configure config/config. Default. Js
Security = {// Disable CSRF CSRF: {enable: false}, // cross-domain whitelist 'http://localhost:3000' domainWhiteList: []} / config/allow cross-domain method. The cors = {allowMethods origin: '*' : 'the GET, PUT, POST, DELETE, PATCH'}Copy the code
4. Post Request parameters
async create() {
const { ctx } = this
let params = ctx.request.body
let res = {
msg: 'ok',
data: {
id: '3',
username: params.username,
nickname: params.nickname,
sex: params.sex
}
}
ctx.body = res
}
Copy the code
The test results
7. Resource routing
According to the previous route writing method, each method needs to write a route to match it, there is no way to group.
'use strict'; const Controller = require('egg').Controller; Class PostController extends Controller {this.ctx.body =' PostController '} class PostController extends Controller {this.ctx.body =' PostController '} Async show() {this.ctx.body =' new page '} // new page async show() {this.ctx.body =' new page '} Async Update () {this.ctx.body =' update logic '} // Delete logic async Export () {this.ctx.body =' exports'}} Module.exports = PostController;Copy the code
Then identify it in router.js
Router. resources('post','/ API /post',controller.post)Copy the code
Request the effect
8. Routing module
1. Create a Route folder and write JS by module
Such as post. Js
module.exports = app => {
const { router, controller } = app;
// user.js
router.get('/user/list', controller.user.list)
router.get('/user/info1/:id', controller.user.info1)
router.get('/user/info2', controller.user.info2)
router.post('/user/create', controller.user.create)
};
Copy the code