-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
52 lines (42 loc) · 1.11 KB
/
main.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
const express = require("express");
const fs = require("fs");
const cors = require("cors");
const app = express();
const PORT = 4444;
let theData = JSON.parse(fs.readFileSync(`${__dirname}/data.json`));
app.use(cors());
// Middleware to add 2-second delay
app.use((req, res, next) => {
setTimeout(next, 2000);
});
// Middleware to handle 50% chance of exception
app.use((req, res, next) => {
if (Math.random() < 0.5) {
throw new Error("Something went wrong");
}
next();
});
// Use express.json middleware to parse JSON bodies
app.use(express.json());
app.get("/get-data", async (req, res) => {
try {
res.json(theData);
} catch (error) {
res.status(500).json({ message: error.message });
}
});
app.post("/store-data", (req, res) => {
try {
theData = req.body;
res.status(200).json({ message: "Data stored successfully!" });
} catch (error) {
res.status(500).json({ message: error.message });
}
});
// Error handling middleware
app.use((err, req, res, next) => {
res.status(500).send({ message: err.message });
});
app.listen(PORT, () => {
console.log(`Server is running on http://localhost:${PORT}`);
});