-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
43 lines (37 loc) · 1.12 KB
/
app.js
File metadata and controls
43 lines (37 loc) · 1.12 KB
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
const koa = require('koa');
const app = new koa();
const static = require('koa-static');
const favicon = require('koa-favicon');
// const tpl = require('./tpl');
const { join } = require('path');
const port = process.env.PORT || 3001;
// set static directiory
app.use(static(join(__dirname, 'dist')));
app.use(favicon(join(__dirname, 'favicon.jpg')));
//if historyApiFallback mode
// app.use(tpl({
// path: join(__dirname, 'dist')
// }));
// app.use(async (ctx, next) => {
// if(ctx.href.search('.js') < 0){
// ctx.render('index.html');
// }
// });
// deal 404
app.use(async (ctx, next) => {
ctx.status = 404;
ctx.body = '404! page not found !';
});
// koa already had middleware to deal with the error, just rigister the error event
app.on('error', (err, ctx) => {
ctx.status = 500;
ctx.statusText = 'Internal Server Error';
if (ctx.app.env === 'development') {
//throw the error to frontEnd when in the develop mode
ctx.res.end(err.stack); //finish the response
} else {
ctx.body = { code: -1, message: 'Server Error' };
}
});
app.listen(port);
console.log('app server running at: http://localhost:%d', port);