点滴记录
自古成功在尝试。

NODE学习笔记六-Koa2实战搭建cms系统2

数据库模块
把读数据,写数据,删除数据封装成方法,并回调取值

/**
 * 数据库模块
 */
const mysql = require('mysql');
const connection = mysql.createConnection({
    host     : 'localhost',
    user     : 'root',
    password : '',
    database : 'cx'
});
connection.connect();
const getData = function(tab){
    return new Promise(function(resolve,reject){
        connection.query("select * from "+tab, function (error, results) {
            if (error)  throw error;
            resolve(results);  
        });
    });
    connection.end(); 
};
const selectData = function(inf,tab,condition){
    return new Promise(function(resolve,reject){
        connection.query("select "+inf+" from "+tab+" where "+condition, function (error, results) {
            console.log("select "+inf+" from "+tab+"where"+condition);
            if (error)  throw error;
            resolve(results);  
        });
    })
}
const addData = function(inf,tab,value){
    return new Promise(function(resolve,reject){
        connection.query("insert into "+inf+" "+tab+" values "+value, function (error, results,fields) {
            console.log("insert into "+inf+" "+tab+" values "+value);
            if (error)  throw error;
            resolve(results.affectedRows);  
        });
    })
}
const delData = function(tab,condition){
    return new Promise(function(resolve,reject){
        connection.query("delete from "+tab+" where "+condition, function (error, results,fields) {
            console.log("delete from "+tab+" where "+condition);
            if (error)  throw error;
            resolve(results.affectedRows);  
        });
    })
}

读文件模块

/**
 * 读取模板模块
 */
const fs = require('fs');
const getHtml =function(filename){
    return new Promise(function(resolve,reject){
        fs.readFile(filename, 'utf8', function(err, data){
            if (err) throw err;
            resolve(data); 
        });
    })
}

路由模块(cookie会话)

const Router = require('koa-router');
const router = new Router();
const s_request = require('sync-request');  /*适合爬取得模块*/
const koabody = require('koa-body');        /*读取post用的模块*/
exports.retGet = function(path,filename){
    for(let i =0;i{
            let html = await getHtml('./view'+filename[i]);
            /* 同步读取法 */
            // let html = fs.readFileSync('./view'+filename[i],'utf-8')
            console.log(filename[i]+" is readed");
            ctx.body = html;
        });
    }
    router.get('/show',async (ctx, next)=>{
        let l = await getData("content");
        let html = await getHtml('./view/show.html');
        let div = html.split("shone");
        let div0 = div[0];
        let div1 = div[1];
        let ndiv1 = "";
        let div2 = div[2];
        let sm = div1.split("shppp");
        for(let i =0;i{
        let id = ctx.params;
        let res = await selectData("*","content"," id='"+id.id+"'");
        let html = await getHtml('./view/more.html');
        let h=html.split("本网站真在建设中......");
        ctx.body = h[0]+res[0].title+h[1]+res[0].content+h[2]+res[0].writer+h[3];
    })
    router.get('/del',async (ctx, next)=>{
        let value = ctx.request.header.cookie;
        if(value!=null){
            let l = await getData("content");
            let html = await getHtml('./view/del.html');
            let div = html.split("shone");
            let div0 = div[0];
            let div1 = div[1];
            let ndiv1 = "";
            let div2 = div[2];
            let sm = div1.split("shppp");
            for(let i =0;i{
        let id =ctx.params.id; 
        let value = ctx.request.header.cookie;
        if(value!=null){
            let res = await delData("content"," id = '"+id+"'")
            ctx.body=res;
        }else{
            ctx.redirect('/');
        }
    });
    router.get('/doget',async(ctx,next)=>{
        ctx.body = ctx.query;
    });
    router.get("/select",async ctx=>{
        let data = await getData("content");
        ctx.body=data;
    });
    // router.get('/show',async(ctx,next)=>{
    //      let res = s_request('GET','http://localhost:3000/doget?uname=123');
    //      ctx.body = res.getBody('utf8');
    // });
    router.get('/login',koabody(),async (ctx,next)=>{
        ctx.body = await getHtml('./view/login.html')
    });
    router.post('/login',koabody(),async(ctx,next)=>{
        let uname = ctx.request.body.uname;
        let upwd = ctx.request.body.upwd;
        let data = await selectData("*","admin","uname='"+uname+"' and upwd='"+upwd+"'");
        if(data[0]!=null){
           ctx.cookies.set(
                'id', 
                'chen',
                {
                domain: 'localhost',  // 写cookie所在的域名
                path: '/add',       // 写cookie所在的路径
                maxAge: 10 * 60 * 1000, // cookie有效时长
                expires: Date.now()+10000,  // cookie失效时间
                httpOnly: false,  // 是否只用于http请求中获取
                overwrite: false  // 是否允许重写
                }
            )
            ctx.cookies.set(
                'id', 
                'chen',
                {
                domain: 'localhost',  // 写cookie所在的域名
                path: '/del',       // 写cookie所在的路径
                maxAge: 10 * 60 * 1000, // cookie有效时长
                expires: Date.now()+10000,  // cookie失效时间
                httpOnly: false,  // 是否只用于http请求中获取
                overwrite: false  // 是否允许重写
                }
            )
            ctx.cookies.set(
                'id', 
                'chen',
                {
                domain: 'localhost',  // 写cookie所在的域名
                path: '/add/:id',       // 写cookie所在的路径
                maxAge: 10 * 60 * 1000, // cookie有效时长
                expires: Date.now()+10000,  // cookie失效时间
                httpOnly: false,  // 是否只用于http请求中获取
                overwrite: false  // 是否允许重写
                }
            )
            ctx.redirect('/add');
        }else{
            ctx.redirect('/login');
        }
    });
    router.get('/add',async ctx=>{
        let value = ctx.request.header.cookie;
        if(value!=null){
            ctx.body = await getHtml("./view/add.html");
        }else{
            ctx.redirect('/');
        }
    })
    router.post('/add',koabody(),async ctx=>{
        let value = ctx.request.header.cookie;
        if(value!=null){
            let l = await addData("content","(`title`,`content`,`writer`)","('"+ctx.request.body.title+"','"+ctx.request.body.content+"','"+ctx.request.body.writer+"')");
            console.log(ctx.request.body);
            ctx.redirect('/add');
        }else{
            ctx.redirect('/');
        }
    })
    router.get('*', async (ctx, next) => {
        ctx.body = "404";
    });
}

创建路由接口

/**
 * 路由接口
 */
exports.retRoutes = function(){
    return router.routes();
}
exports.retAllowedMethods = function(){
    return router.allowedMethods();
}

以上就是controller.js的内容

未经允许不得转载:日常笔记 » NODE学习笔记六-Koa2实战搭建cms系统2

评论 抢沙发

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址