-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
executable file
·52 lines (46 loc) · 1.46 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
//Creat DB connection
var db = "mongodb://hameed:hameed@ds159963.mlab.com:59963/fccvoting"
//Creat a port for server to listen on
var port = process.env.PORT || 8000;
//Load in router
var router = require ('./routes/api');
//Load in node modules
var express = require('express');
var morgan = require('morgan');
var mongoose = require('mongoose');
var bodyParser = require('body-parser');
var dotenv = require('dotenv');
//Creat an express app
var app = express();
//Load in environment variables
dotenv.config({ verbose : true });
//Connect to mongo
mongoose.connect(db, function(err){
if(err) {
console.log(err);
}
});
//Listen to mongoose connection events
mongoose.connection.on('connected', function(){
console.log('successfully opened connection to ' + db);
});
mongoose.connection.on('disconnected', function(){
console.log('successfully disconnected from ' + db);
});
mongoose.connection.on('error', function(){
console.log('An error has occured connecting to ' + db);
});
//Configur express middleware
app.use(morgan('dev'));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false}));
app.use('/node_modules', express.static(__dirname + '/node_modules'));
app.use(express.static(__dirname + '/public'));
app.use('/api', router);
app.get("*", function(request,response){
response.sendFile(__dirname + '/public/index.html');
});
// Satrt up a server
app.listen(port, function(){
console.log('listening on ' + port);
});