-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathserver.js
43 lines (36 loc) · 1.06 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
require("dotenv").config();
const express = require("express");
const app = express();
const PORT = 3000;
const cors = require("cors");
app.use(
cors({
origin: "https://tastetracker5.netlify.app",
methods: ["GET", "POST", "DELETE", "PUT", "PATCH"],
allowedHeaders: ["Content-Type", "Authorization"],
})
);
app.options("*", cors({ origin: "https://tastetracker5.netlify.app" }));
app.use(express.json());
app.use(require("./api/auth").router);
app.use("/my-lists", require("./api/myLists"));
app.use("/list-items", require("./api/listItems"));
app.use("/shopping", require("./api/shopping"));
// Logging middleware
app.use((req, res, next) => {
console.log(`${req.method} ${req.originalUrl}`);
next();
});
// 404
app.use((req, res, next) => {
next({ status: 404, message: "Endpoint not found." });
});
// Error-handling
app.use((err, req, res, next) => {
console.error(err);
res.status(err.status ?? 500);
res.json(err.message ?? "Sorry, something went wrong :(");
});
app.listen(PORT, () => {
console.log(`Listening on port#: ${PORT}...`);
});