-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
48 lines (39 loc) · 1.28 KB
/
app.js
File metadata and controls
48 lines (39 loc) · 1.28 KB
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
/**
* app.js
* exports an Express app as a function
*/
const express = require('express');
const app = express();
const bodyParser = require('body-parser');
//add body parser as middleware for all requests
app.use(bodyParser.json());
//load ENV Variables from config file
const config = require('./config/local');
process.env.APP_PORT = config.APP_PORT;
process.env.DB_HOST = config.DB_HOST;
process.env.DB_USER = config.DB_USER;
process.env.DB_PASS = config.DB_PASS
process.env.JWT_KEY = config.JWT_KEY;
//interact with MongoDB
const mongoose = require('mongoose');
//compose connection details
let dbConn = "mongodb://" + process.env.DB_USER + ":" + process.env.DB_PASS + "@" + process.env.DB_HOST;
//connect to the database
mongoose.connect(dbConn, {useNewUrlParser: true}).then( () => {
console.log('Connected to the database');
}).catch( err => {
console.log('Error connecting to the database: ' + err);
process.exit();
})
//define routes
app.get('/', (req, res) => {
res.send({
message: 'Hello!'
})
})
//import router with endpoints definitions
const routes = require('./api/routes');
//attach router as a middleware
app.use(routes);
app.listen(process.env.APP_PORT, () => console.log(`Example app listening at http://localhost:${process.env.APP_PORT}`))
module.exports = app;