-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.ts
More file actions
44 lines (37 loc) · 1.3 KB
/
app.ts
File metadata and controls
44 lines (37 loc) · 1.3 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
import * as bodyParser from "body-parser";
const path = require('path');
import express from "express";
import routes from "./routes/index";
import { ControllerError } from "./lib/exceptions/controller_exception";
import cors from 'cors';
class App {
public express: express.Application;
constructor() {
this.express = express();
this.middleware();
this.routes();
}
// Configure Express middleware.
private middleware(): void {
this.express.use(bodyParser.json());
this.express.use(bodyParser.urlencoded({ extended: false }));
this.express.use(express.static(path.join(__dirname, '../ui/build')));
this.express.use(cors());
}
private routes(): void {
routes.forEach((route) => {
this.express[route.method](route.path, ...(route.middleware || []), (req, res) => {
route.handler(req)
.then(data => res.json(data))
.catch((e: ControllerError) => {
console.log(e)
res.status(e.status || 400).json({ message: e.message });
})
})
})
this.express.use("*", (req, res, next) => {
res.send("Invalid api route");
});
}
}
export default new App().express;