-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
43 lines (31 loc) · 1.18 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
const express = require('express');
const bodyParser = require('body-parser');
const cors = require('cors');
const path = require("path");
const mongoose = require('mongoose');
//connecting to database using mongoose connect
mongoose
.connect('mongodb+srv://overthere:[email protected]/database?retryWrites=true&w=majority',
{ useNewUrlParser: true, useUnifiedTopology: true })
.catch(e => {
console.error('Connection error', e.message);
});
const db = mongoose.connection;
const route = require('./route');
const app = express();
const port = process.env.PORT || 5000;
app.use(bodyParser.urlencoded({ extended: true }));
app.use(cors());
app.use(express.json());
app.use(bodyParser.json());
app.use('/all', route);
db.on('error', console.error.bind(console, 'Mongodb connection error:'));
app.use((err, req, res, next) => {
res.status(err.status || 500);
res.json({ error: err });
});
app.use(express.static(path.join(__dirname, "client", "build")));
app.get("*", (req, res) => {
res.sendFile(path.join(__dirname, "client", "build", "index.html"));
});
app.listen(port, () => console.log(`Server is up and running on port ${port}`));