yapi/server/app.js

60 lines
1.6 KiB
JavaScript
Raw Normal View History

2017-09-17 13:36:51 +08:00
process.env.NODE_PATH = __dirname;
require('module').Module._initPaths();
const yapi = require('./yapi.js');
const commons = require('./utils/commons');
yapi.commons = commons;
const dbModule = require('./utils/db.js');
2017-09-20 20:21:58 +08:00
yapi.connect = dbModule.connect();
const mockServer = require('./middleware/mockServer.js');
2017-09-17 13:36:51 +08:00
const plugins = require('./plugin.js');
const websockify = require('koa-websocket');
const websocket = require('./websocket.js');
const Koa = require('koa');
const koaStatic = require('koa-static');
const bodyParser = require('koa-bodyparser');
const router = require('./router.js');
2017-11-07 16:39:27 +08:00
let indexFile = process.argv[2] === 'dev' ? 'dev.html' : 'index.html';
2017-09-20 20:21:58 +08:00
2017-09-05 10:24:13 +08:00
const app = websockify(new Koa());
2017-12-19 15:46:37 +08:00
app.proxy = true;
2017-09-05 10:24:13 +08:00
yapi.app = app;
2017-12-19 15:46:37 +08:00
app.use(bodyParser({multipart: true}));
app.use(mockServer);
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( async (ctx, next)=>{
if(ctx.path.indexOf('/prd') === 0){
ctx.set('Cache-Control', 'max-age=8640000000');
if(yapi.commons.fileExist( yapi.path.join(yapi.WEBROOT, 'static', ctx.path+'.gz') )){
ctx.set('Content-Encoding', 'gzip')
ctx.path = ctx.path + '.gz';
}
}
await next()
})
app.use(koaStatic(
yapi.path.join(yapi.WEBROOT, 'static'),
2017-08-23 20:41:47 +08:00
{index: indexFile, gzip: true}
));
app.listen(yapi.WEBCONFIG.port);
2018-02-01 17:46:45 +08:00
commons.log(`the server is start at 127.0.0.1${ yapi.WEBCONFIG.port == '80' ? '' : ':' + yapi.WEBCONFIG.port}`);