vue+express 构建后台管理系统的示例代码
流苏 人气:0一个vue+express 构建的后台管理系统
说明:
vue+express 构建的后台管理系统,包括登录、注册、表格的增删改查
搭建vue项目:
1.安装vue-cli脚手架
npm install -g vue-cli
2.创建基于webpack模版的项目
vue init webpack my-express
3.安装包依赖并运行
cd my-express npm install npm run dev
vue项目基于iview-admin改造的
通过应用生成器工具 express创建一个应用的骨架:
1.连接数据库
在config创建db.js
var mysql = require("mysql"); var connection = mysql.createConnection({ host:"", port: 3306, user:"root", password:"", database:"", useConnectionPooling: true }); function query(sql,data,callback){ // connection.connect() // pool.getConnection(function(err,connection){ connection.query(sql,data,function (err,rows) { callback(err,rows); // connection.release(); // connection.end() }); // }); } exports.query = query;
在routers路由文件下引入
var express = require('express'); var router = express.Router(); var db = require("../confighttps://img.qb5200.com/download-x/db"); const jwt = require('jsonwebtoken') const token = require("../config/token") var data={data:'',meta:{code:'200',message:''}} /* GET users listing. */ router.post('/add', function(req, res, next) { let username = req.body.username; let password = req.body.password; db.query("SELECT username FROM users where username=(?)",[username],function(err,rows){ console.log(err,rows) if(rows.length>0){ data={data:'',meta:{code:'500',message:'用户名存在'}} res.send(data) }else{ db.query("INSERT INTO `users` (`username`,`password`) VALUES (?,?)",[username,password],function(err,rows){ data={data:'',meta:{code:'200',message:'注册成功'}} res.send(data) }); } }); });
2.加入token验证
安装jsonwebtoken
npm install jsonwebtoken
在config创建token.js
const crypto = require('jsonwebtoken') const secret = "JWT-TOKEN" const token={ createToken:function(obj,timeout){ // Token 数据 let payload = { name: obj.username, admin: true }; // 密钥 // 签发 Token let tokens = crypto.sign(payload, secret, { expiresIn: 3600}) return tokens; }, decodeToken:function(tokens){ console.log(tokens) let res = false; crypto.verify(tokens, secret , function(err,decoded) { if(err){ res = {'flag':false,'decoded':decoded} }else{ res = {'flag':true,'decoded':decoded} } }) return res; }, checkToken:function(token){ var resDecode=this.decodeToken(token); if(!resDecode){ return false; } //是否过期 var expState=(parseInt(Date.now()/1000)-parseInt(resDecode.payload.created))>parseInt(resDecode.payload.exp)?false:true; if(resDecode.signature===resDecode.checkSignature&&expState){ return true; } return false; } }; module.exports=exports=token;
在app.js验证token是否过期,过去返回401
app.all('*', function(req, res, next) { res.header("Access-Control-Allow-Origin", "*"); res.header('Access-Control-Allow-Methods', 'PUT, GET, POST, DELETE, OPTIONS'); res.header("Access-Control-Allow-Headers", "Content-Type, access_token, X-Requested-With") // res.header("Content-Type", "application/json;charset=utf-8"); console.log(req.originalUrl,'11111') if(rouetpass.indexOf(req.originalUrl) > -1 || req.originalUrl.split('/').indexOf('static') > -1){ next() }else{ if (req.method != "OPTIONS"){ var accesstoken = req.headers['access_token']; let datatoken = token.decodeToken(accesstoken) // console.log(data) if(datatoken.flag){ next() }else{ data.meta.code=401; res.send(data) } }else{ next() } } });
项目部署:
1.将vue项目打包后放在express项目public文件夹下,通http://localhost:3000即可以访问。
2.部署阿里云
创建实例
添加安全组允许3000端口
使用putty连接linux服务器,将express项目压缩上传
加载全部内容