-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
55 lines (44 loc) · 1.45 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
// Require Express to run server and Routes
const express = require('express')
// Loading env variables
require("dotenv").config();
// Dependencies
const bodyParser = require('body-parser')
const path = require('path')
const morgan = require('morgan')
const database = require('./db')
const errorHandler = require('./src/middlewares/error-handler')
const router = require('./src/routers/index')
let isProduction = (process.env.NODE_ENV == 'production')
// Start up an instance of the app
const app = express()
// Setting the view engine
app.set('view engine', 'ejs')
// Middle-wares
app.use(bodyParser.urlencoded({ extended: false }))
app.use(express.json())
app.use(morgan('dev'));
app.use(router);
// Initializing the Project Main Folder
app.use(express.static(path.join(__dirname, './public')))
/// catch 404 and forward to error handler
app.use(function (_req, res, _next) {
var err = new Error('Not Found');
err.status = 404;
res.status(404).json({ message: err.message })
});
// Error handler middleware
app.use(errorHandler)
// Setup Server
const port = process.env.PORT || 8080;
// Connecting to the database then firing the server
database.connect()
.then(() => {
console.log('Connected to DB successfully')
app.listen(port, () => console.log(`server is running on port ${port}`));
})
.catch((err) => {
console.log("Could'nt connect to DB")
if (!isProduction)
console.log(err.stack)
})