-
Notifications
You must be signed in to change notification settings - Fork 1
/
app.js
47 lines (39 loc) · 1.24 KB
/
app.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
const dotenv = require("dotenv");
dotenv.config();
const express = require("express");
const path = require("path");
const cookieParser = require("cookie-parser");
const mongoose = require("mongoose");
const indexRouter = require("./routes/index");
const usersRouter = require("./routes/users");
var app = express();
const mongoDB =
"mongodb+srv://sagar:[email protected]/myFirstDatabase?retryWrites=true&w=majority";
mongoose.connect(mongoDB, {
useNewUrlParser: true,
useUnifiedTopology: true,
useCreateIndex: true,
});
const db = mongoose.connection;
db.on("error", console.error.bind(console, "MongoDB connection error:"));
app.use(express.json());
app.use(express.urlencoded({ extended: false }));
app.use(cookieParser());
app.use("/api", indexRouter);
app.use("/api/users", usersRouter);
if (process.env.NODE_ENV === "production") {
app.use(express.static("client/build"));
app.get("*", (req, res) => {
res.sendFile(path.resolve(__dirname, "client", "build", "index.html"));
});
} else {
app.use(
express.static(path.join(__dirname, "client", "public", "index.html"))
);
}
app.get("/read", (req, res) => {
res.send("Hello !!");
});
app.listen(process.env.PORT, () => {
console.log("You are connected !");
});