-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.ts
75 lines (58 loc) · 1.86 KB
/
server.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
import dotenv from 'dotenv';
dotenv.config();
import bodyParser from 'body-parser';
import cors from 'cors';
import express from 'express';
import http from 'http';
import morgan from 'morgan';
import path from 'path';
import logFile from 'ptz-log-file';
const log = logFile({ dir: './logs' });
const env: string = process.env.NODE_ENV || 'developement';
const app = express();
log('starting server');
const PORT = process.env.PORT || 3000;
app.use(cors());
app.use('/app', express.static(path.resolve(__dirname, '../client/app')));
app.use('/libs', express.static(path.resolve(__dirname, '../client/libs')));
// for system.js to work. Can be removed if bundling.
app.use(express.static(path.resolve(__dirname, '../client')));
app.use(express.static(path.resolve(__dirname, '../../node_modules')));
app.use(bodyParser.urlencoded({ extended: false }));
app.use(bodyParser.json());
app.use(morgan('dev'));
const renderIndex = (req: express.Request, res: express.Response) => {
res.sendFile(path.resolve(__dirname, '../client/index.html'));
};
app.get('/*', renderIndex);
if (env === 'developement') {
app.use((error, req: express.Request, res: express.Response, next: express.NextFunction) => {
res.status(error.status || 500);
res.json({
error,
message: error.message
});
});
}
app.use((req: express.Request, res: express.Response, next) => {
const error = new Error('Not Found');
next(error);
});
app.use((error: any, req: express.Request, res: express.Response, next: express.NextFunction) => {
res.status(error.status || 500);
res.json({
error: {},
message: error.message
});
});
(async () => {
try {
const server = http.createServer(app);
app.listen(PORT, async () => {
await server.address().port;
console.log('This express angular app is listening on port:' + PORT);
});
} catch (e) {
log(e);
}
})();