-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
41 lines (29 loc) · 1.2 KB
/
app.js
File metadata and controls
41 lines (29 loc) · 1.2 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
const express = require('express');
const mongoose = require('mongoose');
const cors = require("cors")
const userRoutes = require("./routes/user-routes");
const errorController = require("./controllers/error");
const adminRoutes = require("./routes/admin-routes");
// const PORT = 5000;
const server = express();
server.use(express.json());
server.use((req, res, next) => {
res.setHeader("Access-Control-Allow-Origin", "*");
res.setHeader("Access-Control-Allow-Methods", "POST, PATCH, GET, DELETE");
res.setHeader("Access-Control-Allow-Headers", "Authorization, Content-Type");
next();
});
server.use(cors())
server.use(userRoutes);
server.use("/admin", adminRoutes) // work on the connection later with the routes and controller and admin model
server.use(errorController);
const url = `mongodb+srv://${process.env.DB_NAME}:${process.env.DB_PASSWORD}@cluster0.xhbwvcs.mongodb.net/${process.env.COLLECTION}?retryWrites=true&w=majority`;
mongoose.connect(url)
.then(res => {
server.listen(process.env.PORT, () => {
console.log(`app is serving on http://localhost:${process.env.PORT}`);
})
})
.catch(err => {
// console.log("error occur");
});