-
Notifications
You must be signed in to change notification settings - Fork 1
/
server.ts
78 lines (70 loc) · 1.78 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
76
77
78
require('dotenv-safe').config();
import express from 'express';
import bodyParser from 'body-parser';
import morgan from 'morgan';
import compression from 'compression';
import helmet from 'helmet';
import cors from 'cors';
import passport from 'passport';
const app = express();
import i18n from 'i18n';
import initMongo from './config/mongo';
import path from 'path';
import routes from './app/routes';
import { renderFile } from 'ejs';
import expeditiousCache from 'express-expeditious';
// Setup express server port from ENV, default: 3000
app.set('port', process.env.PORT || 3000);
// Enable only in development HTTP request logger middleware
if (process.env.NODE_ENV === 'development') {
app.use(morgan('dev'));
}
// Redis cache enabled by env variable
if (process.env.USE_REDIS === 'true') {
const cache = expeditiousCache({
namespace: 'expresscache',
defaultTtl: '1 minute',
engine: require('expeditious-engine-redis')({
redis: {
host: process.env.REDIS_HOST,
port: process.env.REDIS_PORT
}
})
});
app.use(cache);
}
// for parsing json
app.use(
bodyParser.json({
limit: '20mb'
})
);
// for parsing application/x-www-form-urlencoded
app.use(
bodyParser.urlencoded({
limit: '20mb',
extended: true
})
);
// i18n
i18n.configure({
locales: ['en', 'es'],
directory: `${__dirname}/locales`,
defaultLocale: 'en',
objectNotation: true
});
app.use(i18n.init);
// Init all other stuff
app.use(cors());
app.use(passport.initialize());
app.use(compression());
app.use(helmet());
app.use(express.static('public'));
app.set('views', path.join(__dirname, 'views'));
app.engine('html', renderFile);
app.set('view engine', 'html');
app.use(routes);
app.listen(app.get('port'));
// Init MongoDB
initMongo();
export default app; // for testing