zoukankan      html  css  js  c++  java
  • ko2处理router和数据库

    const Koa = require('koa');
    const app = new Koa();
    const router = require('koa-router')();
    
    const koaBody = require('koa-body');
    
    
    app.use(async (ctx, next) => {
        console.log(`Process ${ctx.request.method} ${ctx.request.url}...`);
        await next();
    });
    
    router.get('/', async (ctx, next) => {
        ctx.response.body = '<h1>Index</h1>';
    });
    
    
    const static = require('koa-static');
    // 配置静态web服务的中间件
    
    app.use(koaBody({}));
    app.use(require('./routes/testrecord').routes());
    app.use(static(__dirname+'/www'));
    app.use(router.routes()).use(router.allowedMethods())
    
    app.listen(3000);
    const router = require('koa-router')();
    
    var testrecord = require('../model/test_record');
    
    
    let basePath = '/testrecord/';
    
    router.get(basePath, async (ctx, next) => {
        ctx.response.body = '<h1>testrecord</h1>';
    });
    
    router.get(basePath+'list', async (ctx, next) => {
        let testrecords = await testrecord.findAll({});
        ctx.response.body = testrecords;
    });
    
    router.get(basePath+'delete/:id',async (ctx, next)=> {
        const id = ctx.params.id;
        let ret = 0;
        try{
            ret = await testrecord.destroy({where:{id:id}});
        }catch (e) {
            console.error(e)
            ctx.response.body = {code:-1}
            return ;
        }
        console.log(ret);
        ctx.response.body = {code:0}
    
    });
    
    router.post(basePath+'save', async (ctx,  next)=> {
        let row =  ctx.request.body;
        console.log(row)
        let ret = 0;
        try{
            if(row.id == undefined || row.id == null)
                ret = await testrecord.create( row )
            else
                ret = await testrecord.update(row,{where:{id: row.id}});
        }catch (e) {
            console.error(e)
            ctx.response.body = {code:-1}
            return ;
        }
        console.log('ret ',ret);
        ctx.response.body = {code:0}
    
    })
    
    
    
    module.exports = router;
        try{
            if(f.id == undefined || f.id == null) {
                ret = await fault.create(f)
                console.log("-----> ",ret)
                f.id = ret.dataValues.id;
            }
            else
                ret = await fault.update(f,{where:{id: f.id}});
        }catch (e) {
            ctx.response.body = {code:-1}
            return ;
        }
    
        let fbs = await faultbaseline.findAll({where: {
                faultid:f.id
            }});
        for(let i=0;i<fs.length;i++){
            fs[i].faultid = f.id;
            for(let j=0;j<fbs.length;j++){
                let fbRow=fbs[j];
                if(fbRow.faultid==f.id && fbRow.baselineid==fs[i].baselineid)
                    fs[i].id=fbRow.id;
            }
        }
        try{
            let retfb =await faultbaseline.bulkCreate(fs,{
                updateOnDuplicate:['baselinestatus']
            });
    
        }catch (e) {
            ctx.response.body = {code:-1}
            return ;
        }
        ctx.response.body = {code:0}
    })
  • 相关阅读:
    30+简约时尚的Macbook贴花
    20+非常棒的Photoshop卡通设计教程
    20+WordPress手机主题和插件【好收藏推荐】
    75+精美的网格网站设计欣赏
    TopFreeTheme精选免费模板【20130629】
    45个有新意的Photoshop教程和技巧
    30个高质量的旅游网站设计
    55个高质量的Magento主题,助你构建电子商务站点
    一个弹框引起的彻夜加班
    开始跟踪Redis啦,开帖
  • 原文地址:https://www.cnblogs.com/cnchengv/p/10797185.html
Copyright © 2011-2022 走看看