following along part 3
This commit is contained in:
parent
2e3dfd4114
commit
ade19f00bc
602 changed files with 67987 additions and 0 deletions
42
parts/3/followAlong/index.js
Normal file
42
parts/3/followAlong/index.js
Normal file
|
|
@ -0,0 +1,42 @@
|
|||
const express = require("express");
|
||||
const app = express();
|
||||
|
||||
let notes = [
|
||||
{ id: "1", content: "HTML is easy", important: true },
|
||||
{ id: "2", content: "Browser can execute only JavaScript", important: false },
|
||||
{
|
||||
id: "3",
|
||||
content: "GET and POST are the most important methods of HTTP protocol",
|
||||
important: true,
|
||||
},
|
||||
];
|
||||
|
||||
app.get("/", (request, response) => {
|
||||
response.send("<h1>Hello World!</h1>");
|
||||
});
|
||||
|
||||
app.get("/api/notes", (request, response) => {
|
||||
response.json(notes);
|
||||
});
|
||||
|
||||
app.get("/api/notes/:id", (request, response) => {
|
||||
const id = request.params.id;
|
||||
const note = notes.find((note) => note.id === id);
|
||||
if (note) {
|
||||
response.json(note);
|
||||
} else {
|
||||
response.status(404).end();
|
||||
}
|
||||
});
|
||||
|
||||
app.delete("/api/notes/:id", (request, response) => {
|
||||
const id = request.params.id;
|
||||
notes = notes.filter((note) => note.id !== id);
|
||||
|
||||
response.status(204).end();
|
||||
});
|
||||
|
||||
const PORT = 3001;
|
||||
app.listen(PORT, () => {
|
||||
console.log(`Server running on port ${PORT}`);
|
||||
});
|
||||
Loading…
Add table
Add a link
Reference in a new issue