-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
48 lines (38 loc) · 1.33 KB
/
Copy pathserver.js
File metadata and controls
48 lines (38 loc) · 1.33 KB
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
import express from "express";
import cors from "cors";
import fs from "fs";
import path from "path";
const app = express();
const PORT = process.env.PORT || 3001; // You can use any port you prefer
app.use(express.json());
app.use(cors()); // Allow cross-origin requests
const notesFfilePath = path.join(process.cwd(), "notes.json");
//fs.writeFileSync
// Get all notes
app.get("/api/notes", (req, res) => {
const data = JSON.parse(fs.readFileSync(notesFfilePath));
res.json(data);
});
// Add a new note
app.post("/api/notes", (req, res) => {
const data = JSON.parse(fs.readFileSync(notesFfilePath));
const newNote = req.body;
data.push(newNote);
fs.writeFileSync(notesFfilePath, JSON.stringify(data, null, 2));
res.status(201).json(newNote);
});
// Ddelete a note
app.delete("/api/notes", (req, res) => {
const data = JSON.parse(fs.readFileSync(notesFfilePath));
const noteId = req.query.id;
const newNote = data.filter((note) => note.id != noteId);
fs.writeFileSync(notesFfilePath, JSON.stringify(newNote, null, 2));
console.log(noteId, newNote);
res.status(201).json({ message: "Note deleted successfully" });
});
app.get("/api/version", (req, res) => {
res.status(200).json({ message: "this is to test the pipeline", value: 1 });
});
app.listen(PORT, () => {
console.log(`Server is running on port ${PORT}`);
});