This repository has been archived by the owner on Apr 2, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
app.ts
75 lines (60 loc) · 2.32 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
import * as express from 'express';
import * as enrouten from 'express-enrouten';
import * as config from 'config';
import * as compression from 'compression';
import * as cookieParser from 'cookie-parser';
import * as bodyParser from 'body-parser';
import * as path from 'path';
import configureProxies from './lib/booting/proxies';
import configureRedirects from './lib/booting/redirects';
import { redirectToLanguage } from './lib/translate';
import configureNunjucks from './lib/booting/nunjucks';
import configureSass from './lib/booting/compileSass';
import configureErrorPages from './lib/booting/errorPages';
class App {
public app: express.Application;
public constructor() {
this.app = express();
this.app.locals = {
...this.app.locals,
...config.get('locals'),
locales: config.get('locales')
};
}
public async setupApp(): Promise<express.Application> {
this.configureExpress();
configureNunjucks(this.app);
await configureSass(this.app);
await this.configureRoutes();
configureErrorPages(this.app);
console.log('App started with:');
console.log('- Node.js', process.version);
console.log(`- Started with NODE_ENV=${process.env.NODE_ENV}`);
return this.app;
}
private configureExpress(): void {
this.app.use(compression());
this.app.use(bodyParser.json());
this.app.use(bodyParser.urlencoded({ extended: false }));
this.app.use(cookieParser());
this.app.disable('x-powered-by');
}
private async configureRoutes(): Promise<void> {
configureProxies(express, this.app);
const publicFolder: string = path.resolve(__dirname, '../src/public');
this.app.use(express.static(publicFolder));
const langRoute = '/:lang';
this.app.get('/', (req: express.Request, res: express.Response): void => {
redirectToLanguage(req, res, '/');
});
this.app.use(langRoute, (req: express.Request, res: express.Response, next: express.NextFunction): void => {
req.lang = req.params.lang;
next();
});
this.app.use('*', configureRedirects);
this.app.use(langRoute, enrouten({
directory: 'controllers'
}));
}
}
export default new App();