npm install -g egg npm install -g egg-init mkdir website-server cd website-server egg-init --type=simple npm install
npm install --save egg-mongoose
'use strict'; /** @type Egg.EggPlugin */ module.exports = { mongoose: { enable: true, package: 'egg-mongoose', }, };
/* eslint valid-jsdoc: "off" */ 'use strict'; /** * @param {Egg.EggAppInfo} appInfo app info */ module.exports = appInfo => { /** * 省略未修改代码 */ // add your middleware config here config.middleware = []; config.mongoose = { client: { url: 'mongodb://127.0.0.1:27017', options: {}, } }; // add your user config here const userConfig = { // myAppName: 'egg', }; return { ...config, ...userConfig, }; };
npm run dev
/** 省略未修改的代码 */ module.exports = appInfo => { /** 省略未修改的代码 */ // add your user config here const userConfig = { security: { csrf: { enable: false } } }; return { ...config, ...userConfig, }; };
'use strict'; /** * @param {Egg.Application} app - egg application */ module.exports = app => { const { router, controller } = app; /** 将路由未“/register”的POST请求交给user控制器中的register方法处理 */ router.post('/register', controller.user.register); };
const Controller = require('egg').Controller; class UserController extends Controller { async register() { const { ctx } = this; /** ctx。request.body得到网络访问的请求体 */ const { username, password } = ctx.request.body; /** 调用业务逻辑下的register方法 */ await ctx.service.user.register(username, password); ctx.body = { username, password }; } } module.exports = UserController;
'use strict' module.exports = app => { const mongoose = app.mongoose; const Schema = mongoose.Schema; const UserSchema = new Schema({ username: { type: String }, password: { type: String }, create_at: { type: Date, default: Date.now }, update_at: { type: Date, default: Date.now }, }); return mongoose.model('User', UserSchema); };
'use strict' const crypto = require('crypto'); const Service = require('egg').Service; class UserService extends Service { async register(username, password) { /** 获取User模型 */ const user = new this.ctx.model.User(); /** 对密码进行加密 */ const pwd = crypto.createHash('md5').update(password).digest('hex'); user.username = username; user.password = pwd; return user.save(); } } module.exports = UserService;
/** 省略未修改代码 */ module.exports = app => { const { router, controller } = app; router.post('/register', controller.user.register); router.post('/login', controller.user.login); };
/** 省略未修改代码 */ async login(username, password) { const { ctx } = this; let pwd = crypto.createHash('md5').update(password).digest('hex'); /** 调用egg中的findOne方法 */ let user = await ctx.model.User.findOne({ username, password: pwd }); return user; } /** 省略未修改代码 */
/** 省略未修改代码 */ async login() { const { ctx } = this; const { username, password } = ctx.request.body; let user = await ctx.service.user.login(username, password); ctx.body = { status: user?'ok' : 'error'}; } /** 省略未修改代码 */
npm install --save egg-jwt
'use strict'; /** @type Egg.EggPlugin */ module.exports = { mongoose: { enable: true, package: 'egg-mongoose', }, jwt: { enable: true, package: "egg-jwt", }, };
/** 省略未修改代码 */ config.jwt = { secret: "jwt_secret" }; /** 省略未修改代码 */
/** 省略未修改代码 */ async login() { const { ctx } = this; const { username, password } = ctx.request.body; const user = await ctx.service.user.login(username, password); if (user) { const token = await ctx.app.jwt.sign({ username: user.username, id: user._id }, /** 使用JWT配置的secret值作为密钥 */ this.config.jwt.secret, { /** 设置有效时间(秒) */ expiresIn: 3600 } ) ctx.body = { status: 'ok' , token}; } else { ctx.body = { status: 'error', }; } } /** 省略未修改代码 */
module.exports = options => { return async function jwt(ctx, next) { const token = ctx.request.header.authorization; if (token) { let decode = ctx.app.jwt.verify(token ,options.secret); if (decode.exp > parseInt(new Date().getTime() / 1000)) { return await next(); } } ctx.status = 401; ctx.body = {}; return; }; };
至此,注册与登录功能的API完成了
摘取自《React+Node.js实战开发》