Skip to content

Commit 954053b

Browse files
committed
Update repo
1 parent c07dcab commit 954053b

8 files changed

+123
-16
lines changed

.vscode/launch.json

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
{
2+
// Use IntelliSense to learn about possible attributes.
3+
// Hover to view descriptions of existing attributes.
4+
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
5+
"version": "0.2.0",
6+
"configurations": [
7+
{
8+
"type": "node",
9+
"request": "launch",
10+
"name": "Launch Program",
11+
"program": "${workspaceFolder}/hello_crud.js"
12+
}
13+
]
14+
}

express_basic_example_1.js

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
const express = require("express");
2+
const app = express();
3+
4+
/* GET books listing. */
5+
app.get("/books", function(req, res) {
6+
res.send("You requested a list of books....");
7+
});
8+
9+
/* GET a specific book with ID */
10+
app.get("/books/:bookId", function(req, res) {
11+
res.send(`You request information on book ${req.params.bookId}`);
12+
});
13+
14+
/* GET users listing. */
15+
app.get("/users", function(req, res) {
16+
res.send("You requested a list of users....");
17+
});
18+
19+
/* GET a specific user with ID */
20+
app.get("/users/:userId", function(req, res) {
21+
res.send(`You request information on user ${req.params.userId}`);
22+
});
23+
24+
const server = app.listen(3000, function() {
25+
console.log("Application started....");
26+
});
File renamed without changes.
File renamed without changes.

hello_get_and_post.js

+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
const express = require("express");
2+
var bodyParser = require("body-parser");
3+
const app = express();
4+
const PORT = 3000;
5+
6+
app.use(bodyParser.json());
7+
8+
const books = [
9+
{ id: "1", name: "harry potter" },
10+
{ id: "2", name: "a song of ice and fire" },
11+
{ id: "3", name: "lord of the rings" }
12+
];
13+
14+
app.get("/books", function(req, res) {
15+
res.status(200);
16+
res.send(books);
17+
});
18+
19+
app.get("/books/:id", function(req, res) {
20+
const bookId = req.params.id;
21+
const requestedBook = books.filter(book => book.id === bookId);
22+
res.status(200);
23+
res.send(requestedBook);
24+
});
25+
26+
app.post("/books", function(req, res) {
27+
console.log(req.body);
28+
const bookName = req.body.name;
29+
books.push({ id: books.length + 1, name: bookName });
30+
31+
res.status(200);
32+
res.send(books);
33+
});
34+
35+
const server = app.listen(PORT, function() {
36+
console.log(`You're listening to the smooth sounds of port ${PORT}...`);
37+
});

hello_put_and_delete.js

+43
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
const express = require("express");
2+
var bodyParser = require("body-parser");
3+
const app = express();
4+
const PORT = 3000;
5+
6+
app.use(bodyParser.json());
7+
8+
const books = [
9+
{ id: "1", name: "harry potter" },
10+
{ id: "2", name: "a song of ice and fire" },
11+
{ id: "3", name: "lord of the rings" }
12+
];
13+
14+
app.get("/books", function(req, res) {
15+
res.send(books);
16+
});
17+
18+
app.put("/books/:id", function(req, res) {
19+
// 1. get book to be updated
20+
console.log(req.params.id);
21+
let booksWithMatchingId = books.filter(book => book.id === req.params.id);
22+
let bookToBeUpdated = booksWithMatchingId[0];
23+
24+
// 2. update book
25+
bookToBeUpdated["name"] = req.body.name;
26+
27+
// 3. send response with updated book
28+
res.status(200);
29+
res.send(bookToBeUpdated);
30+
});
31+
32+
app.delete("/books/:id", function(req, res) {
33+
// 1. filter out the book which match the id
34+
let remainingBooks = books.filter(book => book.id !== req.params.id);
35+
36+
// 2. send response with remaining books
37+
res.status(200);
38+
res.send(remainingBooks);
39+
});
40+
41+
const server = app.listen(PORT, function() {
42+
console.log(`You're listening to the smooth sounds of port ${PORT}...`);
43+
});

helloworld.js

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
const express = require("express");
22
const app = express();
3+
const PORT = 3000;
34

45
app.get("/", function(req, res) {
56
res.send("Hello World!");
@@ -13,6 +14,6 @@ app.put("/", function(req, res) {
1314
res.send("Got a PUT request");
1415
});
1516

16-
const server = app.listen(3000, function() {
17-
console.log("Application started....");
17+
const server = app.listen(PORT, function() {
18+
console.log(`You're listening to the smooth sounds of port ${PORT}...`);
1819
});

route_example_1.js

-14
This file was deleted.

0 commit comments

Comments
 (0)