koa2+mysql+vue+vant 构建简单版移动端博客
具体内容展示
开始正文
觉得对你有帮助的话,可以star一下^_^
必须安装:
mysql
node.js
vue-cli
目录结构
代码步骤
在 app 目录下 打开 node 运行vue-cli vue init webpack 新建Vue项目
安装以下依赖模块:
"axios": "^0.18.0",
"js-cookie": "^2.2.0",
"js-md5": "^0.7.3",
"nprogress": "^0.2.0",
"vant": "^1.1.15",
"vue": "^2.5.2",
"vue-router": "^3.0.1"
在 koa2 目录下 打开 node 运行 npm init 编写信息
安装以下依赖模块:
"jsonwebtoken": "^8.3.0", // 生成token
"koa-bodyparser": "^4.2.1", // 解析requeset body
"koa-cors": "^0.0.16", // koa跨域
"koa-router": "^7.4.0", // koa-router
"koa-static": "^5.0.0", // koa静态文件
"koa2": "^2.0.0-alpha.7", // koa2
"mysql": "^2.16.0", // mysql
"uuid": "^3.3.2" // 生成userId
在 koa2/config.js 进行 mysql 链接配置
// config.js 数据库配置
module.exports = { // mysql 配置
mysql: {
host: 'localhost', // 地址
user: 'root', // 用户账号
password: '', // 密码
database: 'test' // test库
},
port: 3001 // 监听端口
}
编写 sql 配置
在 koa2/mysql.js 进行编写 sql 语句
// mysql.js 编写sql语句
const mysql = require('mysql');
const config = require('./config.js');
var pool = mysql.createPool(config.mysql);
const query = function (sql, val) {
return new Promise((resolve, reject) => {
pool.getConnection(function (err, connection) {
if (err) {
reject(err)
} else {
connection.query(sql, val, (err, res) => {
if (err) {
reject(err)
} else {