-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.ts
56 lines (51 loc) · 1.45 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
import cors from "cors";
import express, { NextFunction, Response } from "express";
import { INTERNAL_SERVER_ERROR, UNPROCESSABLE_ENTITY } from "http-status";
import logger from "morgan";
import routes from "./api";
import { IRequest } from "./Interfaces";
import tasks from "./tasks/base/TaskBootstrap";
import { isDeployed, isTesting, isProduction } from "./config";
const app = express();
//configure app
if (isDeployed) {
// Trust the proxy to give us the correct client IP address as we are behind AWS ELB not front facing the client
app.set("trust proxy", true);
}
if (!isTesting) {
app.use(logger(isDeployed ? "combined" : "dev"));
}
app.use(
cors({
origin: "*"
})
);
app.use(express.json());
app.use(
express.urlencoded({
extended: true
})
);
app.use("/api", routes);
app.use((err: any, req: IRequest, res: Response, next: NextFunction) => {
console.log("error express middleware block reached");
if (!isProduction) {
console.error(err);
}
if (err.validationError) {
console.log("validation error", err.errors.details[0].message);
return res.status(UNPROCESSABLE_ENTITY).json({
data: null,
message:
err.errors.details[0].message.replace(/"/g, "") || err.message || err
});
}
console.log("general error", err);
const statusCode = err.statusCode || INTERNAL_SERVER_ERROR;
res.status(statusCode).json({
data: null,
message: err.message || err
});
});
tasks.bootstrap();
export default app;