const koa = require('koa'); const path = require('path'); const fs = require('fs'); const mongoose = require('mongoose'); // Load config const config = require('./config.json'); // custom modules const mysql = require('./libs/mysql_pool'); const so = require('./libs/storeObject'); const mem = require('./libs/memcache_lib'); mem.host = 'dyn.trj.tw'; mem.port = 24002; mem.connect(); // 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(); // init mongodb connection let mongoAuth = `${config.db.mongo.user.length > 0 && config.db.mongo.pass.length > 0 ? `${config.db.mongo.user}:${config.db.mongo.pass}@` : ''}`; let mongoUrl = `mongodb://${mongoAuth}${config.db.mongo.host}:${config.db.mongo.port}/${config.db.mongo.dbname}${mongoAuth.length > 0 ? '?authSource=admin': ''}`; mongoose.connect(mongoUrl); // 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'); const logger = require('koa-morgan'); // Koa Main Application const app = new koa(); const server = app.listen(config.port, () => { console.log(`Server start on port ${server.address().port}`); }); // Root Router const router = new Router(); // set app keys app.keys = ['44b4fa5cb8a394294361']; // enable logger app.use(logger('dev')); // enable body parser // app.use(koaBody({ // multipart: true, // // upload file size 10mb // maxFieldSize: 10 * 1024 * 1024 // })); // enable cors app.use(cors()); // enable session app.use(session({ key: 'koa:sess', maxAge: 86400000, overwrite: true, httpOnly: true, signed: true }, app)); // enable static file app.use(koaStatic(path.resolve(__dirname, 'public'))); // enable router app.use(router.routes()); app.use(router.allowedMethods()); // load other route const api_route = require('./route/api'); // set other route router.use("/api", api_route.routes()); router.get('/', async(c, n) => { c.body = 'Get root'; });