-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
52 lines (42 loc) · 1.14 KB
/
index.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
import express from "express";
import dotenv from "dotenv";
import NodeCache from "node-cache";
import status from "./routes/status.routes.js";
import products from "./routes/products.routes.js";
import deals from "./routes/deals.routes.js";
import seller from "./routes/seller.routes.js";
// Define .env config
dotenv.config();
const app = express();
const PORT = process.env.PORT || 3000;
// Define node caching settings > default 15 seconds
const cache = new NodeCache({ stdTTL: 15 });
const verifyCache = (req, res, next) => {
try {
const { id } = req.params;
if (cache.has(id)) {
return res.status(200).json(cache.get(id));
}
return next();
} catch (err) {
throw new Error(err);
}
};
app.use(express.json());
app.use("/", status);
app.use("/status", status);
app.use("/products", verifyCache, products);
app.use("/deals", verifyCache, deals);
app.use("/seller", verifyCache, seller);
app.use("*", (req, res) => {
res.status(400).json({ error: "Not route found" });
});
app.listen(PORT, () => {
try {
console.log(`Server running on port ${PORT}`);
} catch (err) {
console.log(err);
process.exit();
}
});
export default app;