-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
73 lines (62 loc) · 2.35 KB
/
app.js
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
// Depedencies & Libraries
import express from 'express';
import morgan from 'morgan';
import bodyParser from "body-parser";
import cors from 'cors';
import cookieParser from 'cookie-parser';
import dotenv from 'dotenv';
// Routes
import apiRouter from './src/routes/api.js';
import genRouter from './src/routes/gen.js';
import authRouter from './src/routes/auth.js';
// Utils
import logger from './src/utils/logger.js';
// Global variables and Initialization
dotenv.config();
const app = express();
const checkProductionMode = process.env.PRODUCTION_MODE === 'true'
const checkNgrokMode = process.env.NGROK_MODE === 'true'
// remove http for express listen in production mode
const listenUrl = process.env.BACKEND_PRODUCTION_URL.replace('https://', '');
// 1. Middleware for parsing JSON and urlencoded data
app.use(express.json());
app.use(express.urlencoded({ extended: false }));
// 2. Cookie Parser
app.use(cookieParser());
// 3. setup the logger
app.use(morgan('combined', { stream: logger.saveMorganLog() }))
// 4. CORS
app.use(cors());
// 5. support parsing of application/json type post data
app.use(bodyParser.json())
app.use(bodyParser.urlencoded({ extended: true }))
// 6. Static files
app.use(express.static('public'));
// Routing
app.use('/api', apiRouter);
app.use('/gen', genRouter);
app.use('/auth', authRouter);
// Error 404
app.use((req, res, next) => {
res.status(404).send('Sorry cant find that!');
});
if (checkNgrokMode) {
if (checkProductionMode) {
console.error('Ngrok mode is enabled. Please disable it in .env file')
process.exit(1)
}
app.listen(process.env.BACKEND_PORT, () => {
console.log(`${process.env.APP_NAME} listening on port ${process.env.BACKEND_PORT} from ${process.env.BACKEND_DEVELOPMENT_URL}`)
console.log(`Ngrok mode is ${process.env.NGROK_MODE}. Backend is listening on ${process.env.BACKEND_PRODUCTION_URL}`)
});
} else {
if (checkProductionMode) {
app.listen(process.env.BACKEND_PORT, listenUrl, () => {
console.log(`${process.env.APP_NAME} listening on port ${process.env.BACKEND_PORT} from ${process.env.BACKEND_PRODUCTION_URL}`)
});
} else {
app.listen(process.env.BACKEND_PORT, () => {
console.log(`${process.env.APP_NAME} listening on port ${process.env.BACKEND_PORT} from ${process.env.BACKEND_DEVELOPMENT_URL}`)
});
}
}