-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
56 lines (47 loc) · 1.51 KB
/
server.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
const fs = require("fs");
const path = require("path");
const express = require("express");
const mongoose = require("mongoose");
const connectConfig = {
useNewUrlParser: true,
useUnifiedTopology: true,
useCreateIndex: true,
};
const dotenv = require("dotenv").config();
const CONNECT_MONGODB_ATLAS = process.env.CONNECT_MONGODB_ATLAS;
const HttpError = require("./models/http-error");
const placesRoutes = require("./routes/places-routes");
const usersRoutes = require("./routes/users-routes");
const app = express();
app.use(express.json());
app.use("/uploads/images", express.static(path.join("uploads", "images")));
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, DELETE");
next();
});
app.use("/api/places", placesRoutes);
app.use("/api/users", usersRoutes);
app.use((req, res, next) => {
throw new HttpError("Could not find this route.", 404);
});
app.use((error, req, res, next) => {
if (req.file) {
fs.unlink(req.file.path, (err) => {
return next(error);
});
}
if (res.headerSent) {
return next(error);
}
res.status(error.code || 500);
return res.json({ message: error.message || "An unknown error occurred !" });
});
mongoose
.connect(CONNECT_MONGODB_ATLAS, connectConfig)
.then(() => app.listen(process.env.PORT || 5000))
.catch((err) => console.log(err));