-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
48 lines (28 loc) · 1.07 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
import express from 'express';
import mongoose from 'mongoose';
import bodyParser from 'body-parser';
import morgan from 'morgan';
const polls = require( './app/routes/poll')
const users= require( './app/routes/user')
const app = express();
const port = process.env.PORT || 8080;
mongoose.Promise = global.Promise;
mongoose.connect('mongodb://localhost/votingappdb');
const db = mongoose.connection;
db.on('error', console.error.bind(console, 'connection error:'));
app.use(bodyParser.urlencoded({ extended: false}));
app.use(bodyParser.json());
app.use(morgan('dev'));
app.use((req, res, next) => {
res.header("Access-Control-Allow-Origin", "*");
res.header('Access-Control-Allow-Methods', 'GET,POST,DELETE,PUT');
res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
next();
});
app.use(users,polls);
// ...For all the other requests just sends back the Homepage
app.route("*").get((req, res) => {
res.sendFile('client/dist/index.html', { root: __dirname });
});
app.listen(port);
console.log(`listening on port ${port}`);