-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
54 lines (36 loc) · 1.26 KB
/
index.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
"use strict";
// importing our dependencies
import express from "express";
import bodyParser from "body-parser";
import dotenv from "dotenv";
import logger from "morgan";
import mongoose from 'mongoose';
import cors from "cors";
// importing our files
import router from "./app/routes";
// injecting environment variables
dotenv.config();
// initializing server requirments
const port = process.env.PORT || 3000;
const app = express();
// to log every request to the console
app.use(logger('dev'));
// set static files (css and images, etc) location
app.use(express.static('public'));
// setting up body parser
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
// enabling cors on all routes
app.use(cors());
// setting up routes for our app
// we made router a function so that any instance can be passed from here
// should be defined just before starting the server
app.use('/', router(express));
// making DB connection
mongoose.connect(`mongodb://localhost/${process.env.DB}`);
const db = mongoose.connection;
db.on('error', console.error.bind(console, 'connection error:'));
db.once('open', () => {
// start the server if only db connection is succesfull
app.listen(port, () => console.log(`App started at: ${port}`) );
});