Egg series of registered login interface implementation

  • Origin:

Recently, I changed my job and went to an Internet company from the second factory of emplacement. I was still engaged in web front-end development. Now I find that the front-end mainly uses React, Vue and related ecological codes to write business codes, which are basically the same. If there is no bright spot in the project, we should make breakthroughs in other aspects, such as contributing open source or building wheels by ourselves. Node is already one of the necessary skills of front-end engineers. Therefore, THE series I produced are using Egg to build background and ANTD Pro to build front-end interface. Written before a series is antd pro, recently for a period of time before the interview for a month, not timely update, now began to make up, and I found a little egg documents found or many new fields for me, this is the back end system knowledge, look at the document and system in the field of a good way to learn, to make up.

  • Configure an egg:

background:

File directory:

A few words about the intelligence of folders: controller is the external business interface; Extend is a tool-like collection; Middleware is middleware; A model is an API wrapper around a database; The rest of the logs class is basically something, familiar with the function of the folder, when adding files can continue to add according to the same function;

To write an interface, first design the database, user table fields:

The database is operated using the Egg-Sequlize ORM framework, which is a plugin wrapper of Sequlize. Configure the code in config first:

Config. sequelize = {dialect: 'mysql', host: '127.0.0.1', port: 3306, database: 'egg-sequelize-doc-unittest',};Copy the code

The mysql database is used. Once configured, write the basic SQL wrapper statement in the Model folder,

app/model/user.js;

module.exports = app => { const { STRING, INTEGER, DATE } = app.Sequelize; // User model; const User = app.model.define('user', { id: { type: INTEGER, primaryKey: true, autoIncrement: true, }, name: STRING(30), age: INTEGER, created_at: DATE, updated_at: DATE, password: STRING(100), }); User.prototype.associate = function() { app.model.User.hasMany(app.model.Post, { as: 'posts' }); }; return User; };Copy the code

Define an entity that encapsulates the methods at the service layer, using the Model method:

'use strict'; const Service = require('egg').Service; class User extends Service { async list({ offset = 0, limit = 10 }) { return this.ctx.model.User.findAndCountAll({ offset, limit, order: [[ 'created_at', 'desc' ], [ 'id', 'desc' ]], }); } async find(id) { const user = await this.ctx.model.User.findByPk(id); if (! user) { this.ctx.throw(404, 'user not found'); } return user; } async create(user) { return this.ctx.model.User.create(user); } async update({ id, updates }) { const user = await this.ctx.model.User.findByPk(id); if (! user) { this.ctx.throw(404, 'user not found'); } return user.update(updates); } async del(id) { const user = await this.ctx.model.User.findByPk(id); if (! user) { this.ctx.throw(404, 'user not found'); } return user.destroy(); } } module.exports = User;Copy the code

The above service calls the Model API to realize the upper level business, and then calls the output JSON data in the Controller.

app/controller/user.js

'use strict'; const Controller = require('egg').Controller; class UserController extends Controller { async login() { const ctx = this.ctx; Const {name, password} = ctx.request.body; // Const {name, password} = ctx.request.body; let message = '', data = {}; Const user = await ctx.model.user. findOne({where: {name,},}); if (! User) {message = 'user does not exist '; } else if (password ! == user.password) {message = 'password error '; } else {message = 'Login succeeded '; data = { id: user.id }; } ctx.body = { message, data, }; } async register() { const ctx = this.ctx; const { name, password, age } = ctx.request.body; let data = {}; let message = ''; const res = await ctx.model.User.findOne({ where: { name, }, }); If (res) {message = 'user already exists '; } else { const insertresult = await ctx.service.user.create({ name, password, age }); if (! Insertresult) {message = 'failed to register '; } else {message = 'registered successfully '; data = insertresult; } } ctx.body = { message, data, }; } } module.exports = UserController;Copy the code

The controller calls the service method, and finally passes

ctx.body = {}
Copy the code

To output JSON data;

Above the realization of the most basic registration, login function. Follow-up plan to implement restful apis. The most basic interface set up, then began to use VUE to build interface;

About me: Yingbin, Micro store Web front-end coder, front-end attack boy. Personal WeChat: Yingbin192;