const Koa = require('koa')
const Router = require('koa-router')
const mysql = require('mysql')
const co = require('co-mysql')
let conn = mysql.createPool({ host: 'localhost', user: 'root', password: '123456', database: 'jol' })
let server = new Koa()
server.listen(8088)
server.context.db = co(conn)
server.use(async (ctx, next) => {
ctx.set('Access-Control-Allow-Origin', '*')
await next()
})
let router = new Router()
router.use('/api', require('./routers/api'))
server.use(router.routes())
index.js
const Router = require('koa-router')
const Sequelize = require('sequelize');
const sequelize = new Sequelize('jol', 'root', '123456', {
host: 'localhost',
dialect: 'mysql' ,
});
let router = new Router()
router.get('/problem', async ctx => {
ctx.body = await ctx.db.query('SELECT * FROM problem')
})
router.get('/problem/:id', async ctx => {
let { id } = ctx.params
ctx.body = await ctx.db.query('SELECT * FROM problem WHERE problem_id=?', [id])
})
router.get('/catalog/:parent', async ctx => {
let { parent } = ctx.params
ctx.body = await ctx.db.query('SELECT ID, title FROM catalog_table WHERE parentID=?', [parent])
})
router.get('/problem/:id', async ctx => {
let { id } = ctx.params
ctx.body = await ctx.db.query('SELECT * FROM problem WHERE problem_id=?', [id])
})
module.exports = router.routes()
网友评论