-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
44 lines (37 loc) · 1.11 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
//importing modules
const express = require('express');
const mongoose = require('mongoose');
const bodyParser = require('body-parser');
const cors = require('cors');
const path = require('path');
var port = 8000;
const url = 'mongodb://stockPingApp:[email protected]:35352/stock-ping-db';
const app = express();
//middleware to parse data
//important as the POST request body will be undefined if not initialized as below
app.use(cors());
app.use(bodyParser.urlencoded({
extended: false
}));
app.use(bodyParser.json());
const route = require('./routes/route');
//connect to mongodb (mLab)
mongoose.connect(url, {
useNewUrlParser: true
});
//mongodb event messages
mongoose.connection.on('connected', function () {
console.log('---connected to mLab (DB)---');
});
mongoose.connection.on('error', function (err) {
if (err) {
throw err;
}
});
//specify route for REST api
app.use('/api', route);
//static files like index.html
app.use(express.static(path.join(__dirname, 'public')));
app.listen(process.env.PORT || port, function () {
console.log(`---Server running on ${port}---`);
});