-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
38 lines (27 loc) · 848 Bytes
/
server.js
File metadata and controls
38 lines (27 loc) · 848 Bytes
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
require('dotenv').config()
const express = require('express');
const mongoose = require('mongoose');
var cors = require('cors')
const pollsRouter = require('./routes/polls');
//const url = 'mongodb://localhost/VoteAppDB';
const url = process.env.DATABASE_URL;
const app = express();
app.use(cors())
mongoose.connect(url, { useNewUrlParser: true, useUnifiedTopology: true });
const db = mongoose.connection;
db.once('open', () => {
console.log('Connected!');
})
db.on('error', (error) => console.error(error))
app.use(express.json());
app.get('/', async (req, res) => {
try {
res.json({ "test": "it's working" });
} catch (err) {
res.status(500).json({ message: err.message })
}
})
app.use('/polls', pollsRouter);
app.listen(process.env.PORT || 9000, () => {
console.log("Server Started at Port ");
})