yapi/server/app.js

41 lines
1.0 KiB
JavaScript
Raw Normal View History

import yapi from './yapi.js';
import commons from './utils/commons';
yapi.commons = commons;
import dbModule from './utils/db.js';
import mockServer from './middleware/mockServer.js';
import Koa from 'koa';
import koaStatic from 'koa-static';
import bodyParser from 'koa-bodyparser';
import router from './router.js';
import websockify from 'koa-websocket';
import websocket from './websocket.js'
yapi.connect = dbModule.connect();
const app = websockify(new Koa());
let indexFile = process.argv[2] === 'dev' ? 'dev.html' : 'index.html';
app.use(mockServer);
app.use(bodyParser());
app.use(router.routes());
app.use(router.allowedMethods());
websocket(app);
app.use( async (ctx, next) => {
2017-08-18 20:35:31 +08:00
if( /^\/(?!api)[a-zA-Z0-9\/\-_]*$/.test(ctx.path) ){
ctx.path = "/"
await next()
}else{
await next()
}
})
app.use(koaStatic(
yapi.path.join(yapi.WEBROOT, 'static'),
{index: indexFile}
));
app.listen(yapi.WEBCONFIG.port);
commons.log(`the server is start at port ${yapi.WEBCONFIG.port}`);