-
Notifications
You must be signed in to change notification settings - Fork 1
/
server.js
65 lines (58 loc) · 1.49 KB
/
server.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
import express from 'express';
import dotenv from 'dotenv';
import cors from 'cors';
import mongoose from 'mongoose';
import {
DEVELOPMENT_PORT,
DEVELOPMENT_MONGO_URI,
PRODUCTION_PORT,
PRODUCTION_MONGO_URI,
LOCAL_MONGO_URI,
} from './config';
import LOG from './log';
import routes from './routes';
import Handlbars from 'handlebars';
dotenv.config();
const app = express();
const ENVIRONMENT = process.env.NODE_ENV || 'production';
let PORT;
let mongoUri;
app.use(express.json());
app.use(express.urlencoded({ extended: false }));
app.use(cors());
Handlbars.registerHelper('inc', function (value, option) {
return parseInt(value) + 1;
});
// check the environment
if (ENVIRONMENT && ENVIRONMENT.trim() === 'production') {
PORT = PRODUCTION_PORT;
mongoUri = PRODUCTION_MONGO_URI;
LOG.info(`Production Mongo: ${mongoUri}`);
}
if (ENVIRONMENT && ENVIRONMENT.trim() === 'development') {
PORT = DEVELOPMENT_PORT;
mongoUri = LOCAL_MONGO_URI;
LOG.info(`Development Mongo: ${mongoUri}`);
}
if (PORT && mongoUri) {
mongoose
.connect(mongoUri, {
useNewUrlParser: true,
useUnifiedTopology: true,
useCreateIndex: true,
useFindAndModify: false,
})
.then(() => {
LOG.info('Database Synced');
})
.catch((error) => {
LOG.error(error.message);
});
app.get('/', (req, res) => {
res.send(`<h3>School Management REST API</h3>`);
});
app.listen(PORT, () => {
LOG.info(`API is up and running on PORT ${PORT}`);
routes(app);
});
}