-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
31 lines (27 loc) · 856 Bytes
/
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
const express = require("express");
const NodeCache = require("node-cache");
const bodyParser = require("body-parser");
const cors = require("cors");
const app = express();
app.use(bodyParser.json());
app.use(cors());
const cache = new NodeCache({ stdTTL: 100 });
app.get("/todos", (req, res) => {
const todos = cache.get("todos");
if (!todos) return res.status(200).json([]);
return res.status(200).json(todos);
});
app.post("/todos", (req, res) => {
if (!req.body.title || !req.body.description)
return res.status(400).json({ message: "bad request" });
let todos = cache.get("todos");
if (!todos) todos = [];
cache.set("todos", [
...todos,
{ title: req.body.title, description: req.body.description },
]);
return res.status(200).json({ success: true });
});
app.listen(3000, () => {
console.log("runing at 3000");
});