-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
35 lines (28 loc) · 879 Bytes
/
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
require("dotenv").config();
const express = require("express");
const app = express();
const PORT = 3000;
const apiRoutes = require("./API/index");
const cors = require("cors");
//middleware
app.use(express.json());
app.use(require("morgan")("dev"));
app.use(
cors({
origin: "http://localhost:5173", // Only allow your frontend origin
credentials: true, // Allow credentials such as Authorization headers or cookies
})
);
//API routes
app.use("/api", apiRoutes);
//error handling
app.use((error, req, res, next) => {
const statusCode = error.statusCode || 500;
res.status(statusCode).send({ error: error });
// code below will not work. res.status is a function, will always result in undefined.
// res.status(res.status || 500).send({ error: error });
});
//server status log
app.listen(PORT, () => {
console.log(`Listening on port ${PORT}`);
});