-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
34 lines (24 loc) · 894 Bytes
/
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
const express = require("express");
const bodyParser = require("body-parser");
const mongoose = require("mongoose");
const cors = require("cors");
require("dotenv").config();
const mongoURI = process.env.MONGODB_URI;
const app = express(); //initializing express
const items = require("./routes/api"); //importing the route file
//connecting to mongo
mongoose
.connect(mongoURI, { useNewUrlParser: true, useUnifiedTopology: true })
.then(() => console.log("MongoDB Connected"))
.catch((err) => console.log(err));
app.use(cors());
//applying middleware bodyparser
app.use(bodyParser.json());
app.use(express.static(__dirname + "/public"));
if (process.env.NODE_ENV === "production") {
app.use(express.static("client/build"));
}
//using route
app.use("/api", items);
const port = process.env.PORT || 5000;
app.listen(port, () => console.log(`Server running on port: ${port}`));