-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
executable file
·67 lines (57 loc) · 1.64 KB
/
app.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
import bodyParser from "body-parser";
import "dotenv/config";
import express from "express";
import fs from "fs";
import mongoose from "mongoose";
import HttpError from "./models/httpError.js";
import placesRouter from "./routes/places.js";
import usersRouter from "./routes/users.js";
const app = express();
app.use(bodyParser.json());
// CORS
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");
return next();
});
// Static files
app.use("/static", express.static("static"));
app.use("/api/users", usersRouter);
app.use("/api/places", placesRouter);
// Catch unimplemented routes
app.use((req, res, next) => {
return next(new HttpError(404, "Not found."));
});
// Handle errors
app.use((error, req, res, next) => {
if (req.file) {
fs.unlink(req.file.path, (err) => {
if (err) console.error(err);
});
}
if (res.headerSent) {
return next(error);
}
res.status(error.statusCode || 500);
res.json({
ok: false,
message: error.message || "An unknown error occurred.",
data: error.data,
});
});
// Create the image upload dir if it doesn't exist
// https://stackoverflow.com/a/26815894
if (!fs.existsSync(process.env.IMAGE_UPLOAD_DIR)) {
fs.mkdirSync(process.env.IMAGE_UPLOAD_DIR, { recursive: true });
}
const PORT = process.env.PORT || 5000;
mongoose
.connect(process.env.MONGODB_CONNECTION_STRING)
.then(() => {
app.listen(PORT, () => console.log(`Listening on :${PORT}`));
})
.catch((err) => console.error(err));