-
Notifications
You must be signed in to change notification settings - Fork 31
/
Copy pathserver.js
45 lines (38 loc) · 1.13 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
const express = require("express");
const app = express();
const path = require('path')
const mongoose = require("mongoose");
require("dotenv").config();
const bodyParser = require("body-parser");
const cors = require('cors')
app.use(cors())
app.use(bodyParser.urlencoded({ extended: false }));
app.use(bodyParser.json());
app.use(express.json());
var port = process.env.PORT || 8000;
// CONNECTION OF DATABASE WITH MONGOOSE
mongoose.connect(
process.env.DATABASE,
{
useNewUrlParser: true,
useUnifiedTopology: true,
useCreateIndex: true,
useFindAndModify: false,
},
function (error) {
if (error) throw error; // Handle failed connection
console.log("Database connected " + mongoose.connection.readyState);
}
);
// ROUTES AS MIDDLEWARES
app.use("/api", require("./Routes/user"));
app.use("/stocks", require("./Routes/stocks"));
//Serving react
app.use(express.static(path.join(__dirname,'client', 'build')))
app.get('*',(req,res) => {
res.sendFile(path.join(__dirname,'client', 'build', 'index.html'))
})
// LISTENING TO PORT
app.listen(port, (req, res) => {
console.log(`Server has started at port ${port}`);
});