-
Notifications
You must be signed in to change notification settings - Fork 24
Expand file tree
/
Copy pathserver.ts
More file actions
executable file
·84 lines (66 loc) · 1.98 KB
/
server.ts
File metadata and controls
executable file
·84 lines (66 loc) · 1.98 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
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
78
79
80
81
82
83
84
/* eslint-disable jest/require-hook */
import 'reflect-metadata';
import express from 'express';
import compression from 'compression';
import * as bodyParser from 'body-parser';
import * as http from 'http';
import * as path from 'path';
import * as url from 'url';
import 'isomorphic-fetch';
import { logger } from '@cdm-logger/server';
import { websiteMiddleware } from './middlewares/website';
import { corsMiddleware } from './middlewares/cors';
import { errorMiddleware } from './middlewares/error';
import { config } from '../config';
import modules from './modules';
const cookiesMiddleware = require('universal-cookie-express');
let server;
const app = express();
app.use(corsMiddleware);
app.options('*', corsMiddleware);
for (const applyBeforeware of modules.beforewares) {
applyBeforeware(app);
}
app.use(cookiesMiddleware());
// By default it uses backend_url port, which may conflict with graphql server.
const { port: serverPort } = url.parse(config.LOCAL_BACKEND_URL);
// Don't rate limit heroku
app.enable('trust proxy');
if (!__DEV__) {
app.use(compression());
}
app.use(bodyParser.urlencoded({ extended: true }));
app.use(bodyParser.json());
app.use(
'/',
express.static(path.join(__FRONTEND_BUILD_DIR__), {
maxAge: '180 days',
}),
);
app.use(websiteMiddleware);
if (__DEV__) {
app.use(errorMiddleware);
}
server = http.createServer(app);
server.listen(serverPort, () => {
logger.info(`Client Server is now running on port ${serverPort}`);
});
server.on('close', () => {
server = undefined;
});
if ((module as any).hot) {
(module as any).hot.dispose(() => {
try {
if (server) {
server.close();
}
} catch (error) {
logger.error(error.stack);
}
});
(module as any).hot.accept(['./website-antui', './website-chakra-antui', './website-chakra'], () => {
logger.debug('...reloading middleware');
});
(module as any).hot.accept();
}
export default server;