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} })