-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
41 lines (34 loc) · 882 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
32
33
34
35
36
37
38
39
40
41
const express = require("express");
const app = express();
const PORT = process.env.PORT || 3004;
// Mock post data
let posts = [];
app.use(express.json());
// Get all posts
app.get("/posts", (req, res) => {
res.json(posts);
});
// Create a new post
app.post("/posts", (req, res) => {
const newPost = req.body;
posts.push(newPost);
res.status(201).json(newPost);
});
// Update an existing post
app.put("/posts/:id", (req, res) => {
const { id } = req.params;
const updatedPost = req.body;
posts = posts.map((post) =>
post.id === id ? { ...post, ...updatedPost } : post
);
res.json(updatedPost);
});
// Delete a post
app.delete("/posts/:id", (req, res) => {
const { id } = req.params;
posts = posts.filter((post) => post.id !== id);
res.status(204).send();
});
app.listen(PORT, () => {
console.log(`Post service is running on port ${PORT}`);
});