lora-project/server-api/app.js

64 lines
1.4 KiB
JavaScript
Raw Normal View History

2017-06-01 08:32:42 +00:00
const koa = require('koa');
const path = require('path');
const fs = require('fs');
2017-06-01 10:09:25 +00:00
// Load config
const config = require('./config.json');
// custom modules
const mysql = require('./libs/mysql_pool');
// init mysql connection pool
mysql.database = config.db.mysql.dbname;
mysql.host = config.db.mysql.host;
mysql.user = config.db.mysql.user;
mysql.password = config.db.mysql.pass;
mysql.port = config.db.mysql.port;
mysql.createPool();
2017-06-01 08:32:42 +00:00
// Koa Middleware
const session = require('koa-session');
const Router = require('koa-router');
const koaBody = require('koa-body');
const koaStatic = require('koa-static');
const cors = require('kcors');
// Koa Main Application
const app = new koa();
const server = app.listen(3000, () => {
console.log(`Server start on port ${server.address().port}`);
});
// Root Router
const router = new Router();
// enable body parser
app.use(koaBody({
multipart: true,
// upload file size 10mb
maxFieldSize: 10 * 1024 * 1024
}));
// enable cors
app.use(cors());
// enable static file
app.use(koaStatic(path.resolve(__dirname, 'public')));
// enable session
app.use(session({
key: 'koa:sess',
maxAge: 86400000,
overwrite: true,
httpOnly: true,
signed: true
}, app));
app.use(router.routes());
app.use(router.allowedMethods());
2017-06-01 10:09:25 +00:00
// load other route
const api_route = require('./route/api');
// set other route
router.use("/api", api_route.routes());
2017-06-01 08:32:42 +00:00
router.get('/', async(c, n) => {
c.body = 'Get root';
});