Skip to content

Commit 761a7b4

Browse files
Merge pull request #146 from HackYourFuture-CPH/58-create-backend-nodejs-module
[Backend] NodeJS - week 1
2 parents 98760cc + 4468cea commit 761a7b4

24 files changed

+3396
-2
lines changed

courses/backend/node/README.md

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,21 @@
1-
# Node
1+
# Node.js
22

3-
Coming soon
3+
This module is part of the Backend specialism and focuses on using Node.js to build backend services, APIs, and databases. It builds on the Introduction to Backend module from Foundation with more advanced tooling, patterns, and responsibilities.
4+
5+
## Contents
6+
7+
| Week | Topic | Preparation | Lesson Plan | Assignment |
8+
| ---- | ------------------------ | ----------------------------------- | ----------------------------------- | ------------------------------------- |
9+
| 1. | Express | [Preparation](week1/preparation.md) | [Assignment](./week1/assignment.md) | [Session plan](week1/session-plan.md) |
10+
| 2. | Database connection; API | [Preparation](week2/preparation.md) | [Assignment](./week1/assignment.md) | [Session plan](week2/session-plan.md) |
11+
12+
## Module Learning Goals
13+
14+
By the end of this module, you will be able to:
15+
16+
- [ ] Build web servers with Express.js
17+
- [ ] Design and implement APIs using HTTP methods following REST principles
18+
- [ ] Use middlewares for authentication, logging, and validation
19+
- [ ] Test APIs using Postman
20+
- [ ] Use logging and debugging tools to monitor and troubleshoot applications
21+
- [ ] Connect to databases and implement CRUD operations
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import express from "express";
2+
const app = express();
3+
4+
app.use((req, _res, next) => {
5+
console.log(req.headers["accept-language"]);
6+
const isFromDenmark = req.headers["accept-language"].includes("da");
7+
console.log(isFromDenmark);
8+
req.isFromDenmark = isFromDenmark;
9+
10+
next();
11+
});
12+
13+
app.listen(3000, function () {
14+
console.log(`> Ready on http://localhost:3000`);
15+
});
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import express from "express";
2+
const app = express();
3+
4+
// query parameters
5+
// http://localhost:3000/query-parameters?hej=23,%20sd-p
6+
app.get("/query-parameters", (req, res) => {
7+
console.log(req.query);
8+
res.send({ data: req.query });
9+
});
10+
11+
// URL parameters
12+
// http://localhost:3000/parameters/apple-eater
13+
app.get("/parameters/:username", (req, res) => {
14+
console.log(req.params);
15+
res.send({ data: req.params });
16+
});
17+
18+
app.listen(3000, function () {
19+
console.log(`> Ready on http://localhost:3000`);
20+
});
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import express from "express";
2+
const app = express();
3+
4+
app.get("/data", (_req, res) => {
5+
res.send({ data: 1 });
6+
});
7+
8+
app.get("/data", (_req, res) => {
9+
res.send({ data: 2 });
10+
});
11+
12+
// Which one is being called when?
13+
14+
app.get("/data/:id", (req, res) => {
15+
console.log("Parametrized URL");
16+
res.send({ data: req.params });
17+
});
18+
19+
app.get("/data/overview", (req, res) => {
20+
console.log("Overview");
21+
res.send({ data: req.params });
22+
});
23+
24+
app.listen(3000, function () {
25+
console.log(`> Ready on http://localhost:3000`);
26+
});
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import express from "express";
2+
const router = express.Router();
3+
4+
router.get("/", async (_request, response) => {
5+
response.send({ data: "from app.use" });
6+
});
7+
8+
export default router;
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import express from "express";
2+
import router from "./examples/router.js";
3+
4+
const app = express();
5+
6+
app.get("/", (_req, res) => {
7+
res.send({ data: "from app.get" });
8+
});
9+
10+
app.use("/use", router);
11+
12+
app.listen(3000, function () {
13+
console.log(`> Ready on http://localhost:3000`);
14+
});

0 commit comments

Comments
 (0)