forked from Wiredcraft/test-backend
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.ts
77 lines (59 loc) · 1.72 KB
/
app.ts
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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
import express from 'express';
import { AddressInfo } from "net";
import * as path from 'path';
import bodyParser from 'body-parser';
import routes from './routes/index';
import users from './routes/user';
import { system, error } from './lib/logger';
import errorListen from './lib/errorListen';
import db from './db';
/**
* 程序系统错误监听
*/
errorListen();
const debug = require('debug')('my express app');
const app = express();
// 设置模板引擎
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'pug');
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));
app.use(express.static(path.join(__dirname, 'public')));
app.use('/', routes);
app.use('/users', users);
// 404错误处理
app.use((req, res, next) => {
const err = new Error('Not Found');
err['status'] = 404;
next(err);
});
// 开发环境错误处理
if (app.get('env') === 'development') {
app.use((err, req, res, next) => {
error.info(`env_http_request_error:${err.mesage}`);
res.status(err['status'] || 500);
res.render('error', {
message: err.message,
error: err
});
});
}
// 正式环境错误处理
app.use((err, req, res, next) => {
error.info(`http_request_error:${err.mesage}`);
res.status(err.status || 500);
res.render('error', {
message: err.message,
error: {}
});
});
// 设置启动端口
app.set('port', process.env.PORT || 3000);
// 启动监听服务
const server = app.listen(app.get('port'), function () {
let msg: string = `Express server listening on port ${(server.address() as AddressInfo).port}`;
system.info(msg);
debug(msg);
// 初始化数据库
db();
});