NoSQL数据库(五)022-Egg+Redis+MongoDb实现登录流程——业务流程梳理 & 安装依赖与页面编写
业务流程
实现代码
项目初始化
npm init egg --type=simple
项目依赖
- egg-mongoose
- egg-redis
- egg-session
{
"name": "session-redis", "version": "1.0.0", "description": "", "private": true, "egg": {
"declarations": true }, "dependencies": {
"egg": "^2.15.1", "egg-mongoose": "^3.2.0", "egg-redis": "^2.4.0", "egg-scripts": "^2.11.0", "egg-session": "^3.1.0", "egg-view-nunjucks": "^2.2.0" }, "devDependencies": {
"autod": "^3.0.1", "autod-egg": "^1.1.0", "egg-bin": "^4.13.1", "egg-ci": "^1.11.0", "egg-mock": "^3.21.0", "eslint": "^5.13.0", "eslint-config-egg": "^7.1.0" }, "engines": {
"node": ">=10.0.0" }, "scripts": {
"start": "egg-scripts start --daemon --title=egg-server-session-redis", "stop": "egg-scripts stop --title=egg-server-session-redis", "dev": "egg-bin dev", "debug": "egg-bin debug", "test": "npm run lint -- --fix && npm run test-local", "test-local": "egg-bin test", "cov": "egg-bin cov", "lint": "eslint .", "ci": "npm run lint && npm run cov", "autod": "autod" }, "ci": {
"version": "10" }, "repository": {
"type": "git", "url": "" }, "author": "", "license": "MIT" }
插件配置
config.session = {
encrypt: false, signed: false, // maxAge: 10000 } config.redis = {
client: {
port: 6379, // Redis port host: '127.0.0.1', // Redis host password: 'auth', db: 0, } } config.mongoose = {
url: 'mongodb://127.0.0.1/redis-mongoose', options: {
}, // mongoose global plugins, expected a function or an array of function and options }
/ @type Egg.EggPlugin */ module.exports = {
// had enabled by egg // static: {
// enable: true, // } nunjucks: {
enable: true, package: 'egg-view-nunjucks', }, redis: {
enable: true, package: 'egg-redis', }, session: true, mongoose: {
enable: true, package: 'egg-mongoose', } };
路由
module.exports = app => {
const {
router, controller } = app; router.get('/', controller.home.index); router.get('/sign', controller.login.signHtml); router.post('/api/sign', controller.login.sign); router.get('/login', controller.login.loginHtml); router.post('/api/login', controller.login.login); router.get('/logout', controller.login.logout); };
控制器
//app/controller/home.js 'use strict'; const Controller = require('egg').Controller; class HomeController extends Controller {
async index() {
const {
ctx } = this; if (ctx.session.login) {
await ctx.render('home'); } else {
ctx.redirect('/login'); } } } module.exports = HomeController;
// app/controller/login.js 'use strict'; const Controller = require('egg').Controller; class SignController extends Controller {
async signHtml() {
const {
ctx, app } = this; // console.log(user); await ctx.render('sign'); } async sign() {
const {
ctx, app } = this; // console.log('================',ctx.request.body); const requestBody = ctx.request.body; await ctx.model.User.insertMany({
username: requestBody.username, password: requestBody.password }); // console.log(user); ctx.body = '<h1>注册成功!</h1>'; } async loginHtml() {
const {
ctx, app } = this; await ctx.render('login'); } async login() {
const {
ctx, app } = this; const requestBody = ctx.request.body; const inputPassword = requestBody.password; const findUser = await ctx.model.User.find({
username: requestBody.username, }); if (findUser.length && findUser[0].password === inputPassword) {
ctx.session.login = true; ctx.redirect('/'); } else {
ctx.redirect('/login'); } // console.log('findUser', findUser) } async logout() {
const {
ctx, app } = this; ctx.session.login = false; ctx.redirect('/login'); } } module.exports = SignController;
model
// {app_root}/app/model/user.js module.exports = app => { const mongoose = app.mongoose; const Schema = mongoose.Schema; const UserSchema = new Schema({ username: { type: String }, password: { type: String }, }); return mongoose.model('User', UserSchema); }
到此这篇NoSQL数据库(五)022-Egg+Redis+MongoDb实现登录流程——业务流程梳理 & 安装依赖与页面编写的文章就介绍到这了,更多相关内容请继续浏览下面的相关推荐文章,希望大家都能在编程的领域有一番成就!
版权声明:
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。
如若内容造成侵权、违法违规、事实不符,请将相关资料发送至xkadmin@xkablog.com进行投诉反馈,一经查实,立即处理!
转载请注明出处,原文链接:https://www.xkablog.com/sjkxydsj/10797.html