forked from tb1over/node-koa-REST-API
-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
46 lines (33 loc) · 1.31 KB
/
app.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
//APP create@Pangys
const Koa = require('koa');
const bodyParser = require('koa-bodyparser');
const cors = require('koa2-cors');
const controller = require('./controller'); //扫描控制器
const templating = require('./templating'); //模板编译
const rest = require('./rest'); //restAPI
const session = require('./session'); //session配置
const fileServ = require('./file-server');//文件资源服务
//const ws = require('./ws/jnbank');
const app = new Koa();
const isProduction = process.env.NODE_ENV === 'production'; //判断是否为生产环境
app.use(cors()); //跨域-cors
app.use(bodyParser()); //解析请求
app.use(session); //添加session
app.use(async (ctx, next) => { //输出请求
console.log(`${ctx.request.method} ${ctx.request.url}...`); //输出收到的请求
await next();
});
if (!isProduction) { //静态文件加载
let staticFiles = require('./static-file');
app.use(staticFiles('/static/', __dirname + '/static'));
}
app.use(templating('view', { //模板加载
noCache: !isProduction, //非生产->不缓存
watch: !isProduction
}));
app.use(rest.restify()); //REST接口
app.use(controller()); //扫描加载控制器
//ws.ws();
app.listen(3000); // app应用服务在端口3000监听:
fileServ.listen(8099) //文件服务在8099
console.log('app started...');