forked from ArtOfCode-/openletter
-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathapp.ts
86 lines (74 loc) · 2.52 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
78
79
80
81
82
83
84
85
86
import path from 'path';
import sassMiddleware from 'node-sass-middleware';
import layouts from 'ejs-layouts';
import mysql from 'mysql2';
import createDebug from 'debug';
import chalk from 'chalk';
import express, { json, urlencoded } from 'express';
import { ResponseWithLayout } from './definitions';
import { render } from './render_helpers';
import { queries } from './query_helpers';
import { BaseModel } from './models/base';
import config from './config/config';
import { routes } from './config/routes';
const appLogger = createDebug('app:base');
const routesLogger = createDebug('app:routes');
const pool = mysql.createPool(config.database.connectionObject());
BaseModel.pool = pool;
const app = express();
// View engine setup
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'ejs');
app.use(layouts.express);
// Libraries setup: body parser (for POST request bodies), cookie parser, SCSS compilation, static files.
app.use(json());
app.use(urlencoded({ extended: false }));
app.use(sassMiddleware({
src: path.join(__dirname, 'public'),
dest: path.join(__dirname, 'public'),
indentedSyntax: false,
sourceMap: true,
outputStyle: 'compressed'
}));
app.use(express.static(path.join(__dirname, 'public')));
// Request logging setup
app.use((req: express.Request, res: express.Response, next: Function) => {
console.log('\n');
appLogger(`${req.method} ${req.url} HTTP/${req.httpVersion} : ${res.statusCode}`);
next();
});
// Routes
for (const [path, routerFactory] of Object.entries(routes)) {
app.use(path, routerFactory(pool, routesLogger));
}
// Handle errors
app.use((req: express.Request, res: ResponseWithLayout) => {
if (res.statusCode === 500) {
render(
req,
res,
'common/coded_err',
{
name: 'Server Error',
description: 'The server encountered an internal error while serving your request.'
},
{ pool }
);
} else {
render(
req,
res,
'common/coded_err',
{
name: 'Not Found',
description: 'The page you requested could not be found.'
},
{ pool }
);
}
});
(async () => {
await queries(pool, [[], []], "SET GLOBAL sql_mode = 'NO_ENGINE_SUBSTITUTION,ALLOW_INVALID_DATES';", "SET SESSION sql_mode = 'NO_ENGINE_SUBSTITUTION,ALLOW_INVALID_DATES';");
app.listen(config.port);
appLogger(chalk.bold.green(`Listening on ${config.port}.`));
})();