-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.ts
executable file
·80 lines (65 loc) · 2.65 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
import debug from 'debug';
import dotenv from 'dotenv';
import express from 'express';
import * as http from 'http';
import * as winston from 'winston';
import * as expressWinston from 'express-winston';
import cors from 'cors';
import {CommonRoutesConfig} from './common/common.routes.config';
import {UsersRoutes} from './users/users.routes.config';
import { AuthRoutes } from './auth/auth.routes.config';
import helmet from 'helmet';
console.log("process.env", process.env);
if (process.env.NODE_ENV != "production") {
const dotenvResult = dotenv.config();
if (dotenvResult.error) {
throw dotenvResult.error;
}
}
const app: express.Application = express();
const server: http.Server = http.createServer(app);
const port = process.env.PORT || 3000;
const routes: Array<CommonRoutesConfig> = [];
const debugLog: debug.IDebugger = debug('app');
console.log("process.env.PORT:", process.env.PORT);
app.use(helmet());
// here we are adding middleware to parse all incoming requests as JSON
app.use(express.json());
// here we are adding middleware to allow cross-origin requests
app.use(cors());
// here we are preparing the expressWinston logging middleware configuration,
// which will automatically log all HTTP requests handled by Express.js
const loggerOptions: expressWinston.LoggerOptions = {
transports: [new winston.transports.Console()],
format: winston.format.combine(
winston.format.json(),
winston.format.prettyPrint(),
winston.format.colorize({ all: true })
),
};
if (!process.env.DEBUG) {
loggerOptions.meta = false; // when not debugging, log requests as one-liners
if (typeof global.it === 'function') {
loggerOptions.level = 'http'; // for non-debug test runs, squelch entirely
}
}
// initialize the logger with the above configuration
app.use(expressWinston.logger(loggerOptions));
// here we are adding the UserRoutes to our array,
// after sending the Express.js application object to have the routes added to our app!
routes.push(new UsersRoutes(app));
routes.push(new AuthRoutes(app));
// this is a simple route to make sure everything is working properly
const runningMessage = `Server running at http://localhost:${port}`;
app.get('/', (req: express.Request, res: express.Response) => {
console.log("whi is failing??");
res.status(200).send(runningMessage)
});
export default server.listen(port, () => {
routes.forEach((route: CommonRoutesConfig) => {
debugLog(`Routes configured for ${route.getName()}`);
});
// our only exception to avoiding console.log(), because we
// always want to know when the server is done starting up
console.log(runningMessage);
});