-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
57 lines (48 loc) · 1.27 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
const express = require("express");
const mongoose = require("mongoose");
const helmet = require("helmet");
const app = express();
const reviewRouter = require("./routers/reviews");
const userRouter = require("./routers/user");
app.get('/', (req, res) => {
res
.status(200)
.send('Hello server is runningh')
.end();
});
mongoose.connect(
"mongodb+srv://ayo:[email protected]/?retryWrites=true&w=majority",
{
useNewUrlParser: true,
useUnifiedTopology: true,
}
);
const con = mongoose.connection;
con.on("open", () => {
console.log("connected to db");
});
app.use(express.json());
// add helmet to protect the app from some common vulnerabilities
app.use(helmet());
// CORS error
app.use((req, res, next) => {
res.setHeader("Access-Control-Allow-Origin", "*");
res.setHeader(
"Access-Control-Allow-Headers",
"Origin, X-Requested-With, Content-Type, Accept, Authorization"
);
res.setHeader(
"Access-Control-Allow-Methods",
"GET, POST, PATCH, PUT, DELETE, OPTIONS"
);
next();
});
app.get("/", (req, res) => {
res.send("Hello World");
});
app.use("/reviews", reviewRouter);
app.use("/users", userRouter);
const PORT = process.env.PORT || 8080;
app.listen(PORT, () => {
console.log("listening on port 8080");
});