You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
constmysql=require('mysql');constconfig=require('./../config/default');constpool=mysql.createPool({host: config.database.HOST,port: config.database.PORT,user: config.database.USERNAME,password: config.database.PASSWORD,database: config.database.DATABASE});letquery=function(sql,values){returnnewPromise((resolve,reject)=>{pool.getConnection(function(err,connection){if(err){resolve(err);}else{connection.query(sql,values,(err,rows)=>{if(err){reject(err);}else{resolve(rows)}connection.release();})}})})};letusers=`create table if not exists users( id INT NOT NULL AUTO_INCREMENT, name VARCHAR(100) NOT NULL, pass VARCHAR(40) NOT NULL, PRIMARY KEY ( id ));`;letposts=`create table if not exists posts( id INT NOT NULL AUTO_INCREMENT, name VARCHAR(100) NOT NULL, title VARCHAR(40) NOT NULL, content VARCHAR(40) NOT NULL, uid VARCHAR(40) NOT NULL, moment VARCHAR(40) NOT NULL, comments VARCHAR(40) NOT NULL DEFAULT '0', pv VARCHAR(40) NOT NULL DEFAULT '0', PRIMARY KEY ( id ));`;letcomment=`create table if not exists comment( id INT NOT NULL AUTO_INCREMENT, name VARCHAR(100) NOT NULL, content VARCHAR(40) NOT NULL, postid VARCHAR(40) NOT NULL, PRIMARY KEY ( id ));`;letcreateTable=function(sql){returnquery(sql,[]);};createTable(users);createTable(posts);createTable(comment);letinsertData=function(value){let_sql="insert into users(name,pass) values(?,?);";returnquery(_sql,value);};letinsertPost=function(value){let_sql="insert into posts(name, title, content, uid, moment) values(?,?,?,?,?);";returnquery(_sql,value);};letupdatePostComment=function(value){let_sql="update posts set comment=? where id=?";returnquery(_sql,value);};letupdatePostPv=function(value){let_sql="update posts set pv=? where id=? ";returnquery(_sql,value);};letinsertComment=function(value){let_sql="insert into comment(name, content, postid) values(?,?,?);";returnquery(_sql,value);};letfindDataByName=function(name){let_sql=`select * from users where name="${name}"`;returnquery(_sql);};letfindDataByUser=function(name){let_sql=`select * from posts where name="${name}"`;returnquery(_sql);}letfindDataById=function(id){let_sql=`select * from posts where id="${id}"`;returnquery(_sql);}letfindCommentById=function(id){let_sql=`select * from comment where postid="${id}"`;returnquery(_sql);}letfindAllPost=function(){let_sql=`select * from posts`;returnquery(_sql);}letupdatePost=function(values){let_sql=`update posts set title=?,content=? where id=?`;returnquery(_sql,values);}letdeletePost=function(id){let_sql=`delete from posts where id=?`;returnquery(_sql);}letdeleteComment=function(id){let_sql=`delete from comment where id = ${id}`;returnquery(_sql);}letdeleteAllPostComment=function(id){let_sql=`delete from comment where postid = ?`;returnquery(_sql);}letfindCommentLength=function(id){let_sql=`select content from comment where postid in (select id from posts where id=${id})`;returnquery(_sql);}module.exports={
query,
createTable,
insertData,
findDataByName,
findDataById,
findDataByUser,
insertPost,
findAllPost,
insertComment,
findCommentById,
updatePost,
deletePost,
deleteComment,
findCommentLength,
updatePostComment,
deleteAllPostComment,
updatePostPv
};
koa插件选择
路由:koa-router
表单解析:koa-bodyparser
视图:koa-views、ejs
session: koa-session-minimal、koa-mysql-session
数据库引擎: mysql
数据库设计
用户表(users):id、name、pass
文章表(posts):id、name、title、content、uic、moment、comments、pv
评论表(comment):id、name、content、postid
数据库初始化
使用连接池连接数据库,每次查询完毕之后释放链接,可以将数据表的建立在mysql.js中完成,为每一次query创建一个公共函数,
并且每次查询都封装为一个方法,如下:
入口文件设置:
我们应该将数据库等配置放置在一个公共的config文件中,如下:
路由处理
前后端未分离中,存在的问题就是,数据判断逻辑的放置,比如根据用户是否登录显示不同的header,这个可以在视图中判断session.name是否存在,
同样也可以在route中先判断,在给传值为logined:false,个人偏向后一种,毕竟我认为视图中应该尽量少出现逻辑。
完整的教程在这里:http://www.wclimb.site/2017/07/12/Node-Koa2-Mysql-%E6%90%AD%E5%BB%BA%E7%AE%80%E6%98%93%E5%8D%9A%E5%AE%A2/
The text was updated successfully, but these errors were encountered: