-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
83 lines (71 loc) · 2.19 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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
const mongoose = require("mongoose");
const bodyParser = require("body-parser");
const express = require("express");
const userRouter = require("./routers/user");
const recipeRouter = require("./routers/recipe");
const cors = require("cors");
const cookieParser = require("cookie-parser");
const path = require("path");
require("dotenv").config();
const connectionString = process.env.API_KEY;
const app = express();
app.use(cors());
const port = process.env.PORT || 3001;
app.set("port", process.env.PORT || 3001);
// const link =
// process.env.NODE_ENV === "production"
// ? "https://recipefy-g1.herokuapp.com/"
// : `https://localhost:${port}`;
// app.use(express.static(path.join(__dirname + "/public")));
app.use(express.json({ limit: "25mb" }));
app.use(cookieParser());
// app.use(
// cors({
// origin: link,
// credentials: true,
// })
// );
if (process.env.NODE_ENV === "production") {
// Set static folder
app.use(express.static("client/build"));
// app.get("/", (req, res) => {
// res.sendFile(path.resolve(__dirname, "client", "build", "index.html"));
// });
// app.get("/home", (req, res) => {
// res.sendFile(path.resolve(__dirname, "client", "build", "index.html"));
// });
// app.get("/register", (req, res) => {
// res.sendFile(path.resolve(__dirname, "client", "build", "index.html"));
// });
app.get("/*", (req, res) => {
res.sendFile(
path.join(__dirname, "../client/build", "index.html"),
function (err) {
if (err) {
res.status(500).send(err);
}
}
);
});
/*app.get("/forgotpassword", (req, res) => {
res.sendFile(path.resolve(__dirname, "client", "build", "index.html"));
}) */
}
mongoose.connect(connectionString);
app.use(async (req, res, next) => {
try {
return await next();
} catch (error) {
console.log("Error caight in middleware", error);
}
});
// app.use(bodyParser.json());
// app.use(bodyParser.urlencoded({ extended: true }));
app.use(express.urlencoded({ extended: true }));
app.use(express.json());
app.use("/user", userRouter);
app.use("/recipe", recipeRouter);
app.listen(port, () => {
console.log(`Server running at port ${port}`);
});
module.exports = app;