app.js 1.5 KB
/**
 *
 * @
 */

'use strict';

const http = require('http');
const Koa = require('koa');
const mount = require('koa-mount');
const serve = require('koa-static');
const body = require('./middleware/yoho-koa-body');
const convert = require('koa-convert');
const session = require('koa-session');
const compress = require('koa-compress');
const Socket = require('socket.io');

const config = require('./config/config');
const webApp = require('./apps/web');
const ws = require('./lib/ws');
const errorHandle = require('./middleware/error-handle');


const port = process.env.PORT || config.port;

const app = new Koa();
const server = http.createServer(app.callback());
const io = new Socket(server);
ws.init(io);

app.keys = ['yoho-node-ci secret'];

app.use(compress())

app.use(errorHandle(app));
app.use(serve(__dirname + '/public'));
app.use(convert(session({}, app)));
app.use(convert(body({
    multipart: true,
    queryString: {
        plainObjects: true
    },
    jsonLimit: '10mb',
    formLimit: '10mb',
    textLimit: '10mb',
    formidable: {
        keepExtensions: true,
        maxFieldsSize: 10 * 1024 * 1024
    }
})));
app.use(mount('/', webApp));


server.listen(port, () => {
    console.log(`app started in ${port}`);

    // process.on('uncaughtException', function(err) {
    //     console.error('Caught exception:', err.stack)
    // });
    // process.on('unhandledRejection', function(reason, p) {
    //     console.error('Unhandled Rejection at: Promise ', p, ' reason: ', reason.stack)
    // });
});