-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
72 lines (54 loc) · 1.76 KB
/
index.js
File metadata and controls
72 lines (54 loc) · 1.76 KB
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
const express = require("express");
const app = express();
const dotenv = require("dotenv");
dotenv.config();
const PORT = process.env.PORT || 10000;
const cors = require("cors");
const mongoose = require("mongoose");
const itemRoutes = require("./routes/itemRoutes");
const authenticationRoutes = require("./routes/authenticationRoutes");
const { verifyToken } = require("./middlewares/verifyJWT");
const cookieParser = require("cookie-parser");
/* The credentials: true option is crucial as it allows the browser to include credentials
(such as cookies) in the cross-origin request. */
app.use(
cors({
origin: [
"http://localhost:3000",
"https://small-shopping-list.netlify.app",
"https://iiht-shopping-list.vercel.app",
"https://iiht-shopping-list.onrender.com",
"http://172.190.149.196:3000",
],
credentials: true,
exposedHeaders: ["set-cookie"],
})
);
/* This line allows the server to respond to preflight requests sent by the browser
to check the allowed methods and headers for the actual request. */
app.options("*", cors());
app.use(
express.urlencoded({
extended: false,
})
);
app.use(express.json());
app.use(cookieParser());
app.use("/auth", authenticationRoutes);
// Any middleware that comes after verifyToken will go through jwt authentication
// app.use(verifyToken);
app.use("/items", verifyToken, itemRoutes);
app.use("/", (req, res) => {
res.send("Hello, Welcome to Shopping List API");
});
app.listen(PORT, () => console.log(`Server running on PORT ${PORT}`));
mongoose
.connect(process.env.CONNECTION_URL, {
useNewUrlParser: true,
useUnifiedTopology: true,
})
.then(
mongoose.connection.once("open", () => {
console.log("MongoDB Connected Successfully");
})
);