Skip to content

Commit ec04c7f

Browse files
committed
Import 'nodejs' repo
1 parent d944f44 commit ec04c7f

28 files changed

Lines changed: 1667 additions & 2 deletions

legacy/nodejs/README.md

Lines changed: 51 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,52 @@
1-
# nodejs
1+
# NodeJS - Fullstack development
22

3-
TODO
3+
For almost any web application, it is essential to have a backend. The backend is a place where we, the developers, can store our data, communicate with users and let the users communicate with us, do smart things like calculations, data processing etc.
4+
5+
There are many languages for this. We might've heard of Java, C, C++, Go, Python, Ruby, PHP and the list goes on.
6+
There are two reasons why we at HYF choose Node.JS over others:
7+
8+
1. You already know JavaScript, so it's easier to get started than other languages
9+
2. Node.js is great for making web APIs because it is asynchronous by nature and thus allows for high input/output. By this we mean that it allows many users to make very light requests at the same time.
10+
11+
In this 3-week module, we will use NodeJS to create and connect both the backend api and serve the frontend html, css and javascript.
12+
Most of this course will focus on the backend part. Meaning building a webserver that has an api connected to a database.
13+
14+
`Database <--> Webserver`
15+
16+
## Planning
17+
18+
| Week | Topic | Preparation | Homework | Lesson plan |
19+
| ---- | ----------------------------------------------- | ----------------------------------- | ------------------------------------ | ----------------------------------- |
20+
| 1. | HTTP; Introduction to node js; Simple webserver | [Preparation](week1/preparation.md) | [Homework](week1/homework/readme.md) | [Lesson plan](week1/lesson-plan.md) |
21+
| 2. | Express | [Preparation](week2/preparation.md) | [Homework](week2/homework/readme.md) | [Lesson plan](week2/lesson-plan.md) |
22+
| 3. | Database connection; API | [Preparation](week3/preparation.md) | [Homework](week3/homework/readme.md) | [Lesson plan](week3/lesson-plan.md) |
23+
24+
## Pre-requisites
25+
26+
We will build on knowledge from the following HYF (sub)modules. If we feel we have gaps we should review the curriculum ourselves or ask a teacher to help.
27+
28+
- [JavaScript](https://github.com/HackYourFuture-CPH/JavaScript)
29+
- [Git](https://github.com/HackYourFuture-CPH/git)
30+
- [CommandLine](https://github.com/HackYourFuture-CPH/CommandLine)
31+
32+
## What will we learn?
33+
34+
- What is Node.js?
35+
- Using Node Package Manager (NPM)
36+
- Using `import` to include modules
37+
- Using `express` to make a RESTful API
38+
- Using `mysql` to connect the API to the backend
39+
- Postman
40+
41+
## Extra practice
42+
43+
If you have the time, you could follow another simple set of exercises to get a better grasp of Node.js and some more practice.
44+
Feel free to bring any questions from this to the class or raise them on slack!
45+
46+
Clone the repository that holds the exercises templates: <https://github.com/HackYourFuture-CPH/Nodejs-exercise-template.git>
47+
48+
See each week's `readme.md` and follow the instructions to setup the server.
49+
50+
---
51+
52+
_Please help us improve and share your feedback! If you find better tutorials or links, please share them by opening a Pull Request._
34.5 KB
Loading
26.6 KB
Loading
100 KB
Loading
51 KB
Loading
54.1 KB
Loading
39.7 KB
Loading
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
# Server
2+
3+
Create a new, separate folder somewhere on your machine:
4+
5+
```shell
6+
mkdir nodejs-week1
7+
code nodejs-week1 # to open the folder in VS Code
8+
```
9+
10+
You can also reuse some other folder. Main thing to watch out for is that the folder you decide to use should ideally be empty.
11+
12+
---
13+
14+
Initialize and install:
15+
16+
```shell
17+
npm init -y
18+
npm pkg set type="module"
19+
npm install express
20+
echo node_modules/ >> .gitignore
21+
```
22+
23+
Make sure you see a `package.json`.
24+
Make sure you have `"type": "module"` in your `package.json`.
25+
Do you see `express` somewhere in `package.json`?
26+
Make sure you see the `node_modules` folder.
27+
Also make sure that the `node_modules/` folder is ignored by Git.
28+
29+
---
30+
31+
Create a file called `app.js`:
32+
33+
```js
34+
import express from "express";
35+
const app = express();
36+
const port = 3000;
37+
38+
app.get("/", (req, res) => {
39+
res.send("Hello Class!");
40+
});
41+
42+
app.listen(port, () => {
43+
console.log(`Listening on port ${port}`);
44+
});
45+
```
46+
47+
Start your server!
48+
49+
```shell
50+
node app.js
51+
```
52+
53+
Go to <http://localhost:3000> in your browser to verify that everything is working as expected.
54+
55+
Press Ctrl-C to shut down the server.
56+
57+
---
58+
59+
**Task:**
60+
61+
- Add a `/info` route to `app.js`.
62+
- It should respond with a **JSON object** containing the Node.js version; something like this: `{ "nodeVersion": "v17.3.0" }`
63+
- You can get the Node.js version from `process.version`
64+
65+
To test that the response is indeed JSON, you can use Postman or cURL:
66+
67+
```shell
68+
curl -i localhost:3000/info
69+
```
70+
71+
The response `Content-Type` header should look like this: `Content-Type: application/json; charset=utf-8`.
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
# Database
2+
3+
Now we want to connect to a MySQL database from Node.js.
4+
5+
First we need to install some packages:
6+
7+
```shell
8+
npm install mysql2 knex
9+
```
10+
11+
---
12+
13+
Create a new MySQL database named `hyf_node_week1`.
14+
In `app.js` we can now establish the database connection.
15+
Add the following to the top of `app.js`:
16+
17+
```js
18+
import knex from "knex";
19+
const knexInstance = knex({
20+
client: "mysql2",
21+
connection: {
22+
host: "127.0.0.1",
23+
port: 3306,
24+
user: "your_database_user",
25+
password: "your_database_password",
26+
database: "hyf_node_week1",
27+
},
28+
});
29+
30+
// The code from before is down here
31+
// [...]
32+
```
33+
34+
---
35+
36+
Using `knex.raw` we can now execute SQL queries.
37+
Let's extend the `/info` route to also respond with the MySQL version: `{ "nodeVersion":"v17.3.0", "mysqlVersion":"8.0.30" }`
38+
39+
```js
40+
app.get("/info", async (req, res) => {
41+
const [rows] = await knexInstance.raw("SELECT VERSION()");
42+
43+
res.json({
44+
nodeVersion: process.version,
45+
mysqlVersion: rows[0]["VERSION()"],
46+
});
47+
});
48+
```
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
# User routes
2+
3+
## Note on server restarts
4+
5+
It is annoying to shutdown the server with Ctrl-C and start it again every time you make a change and want to test the change:
6+
7+
```shell
8+
npm install --save-dev nodemon
9+
```
10+
11+
Also, it is recommended to add a script to `package.json`, like so:
12+
13+
```json
14+
"scripts": {
15+
"dev": "nodemon app.js"
16+
}
17+
```
18+
19+
There is a command to do that:
20+
21+
```shell
22+
npm pkg set scripts.dev="nodemon app.js"
23+
```
24+
25+
Now you can run
26+
27+
```shell
28+
npm run dev
29+
```
30+
31+
to start a server that automatically restarts when you make changes 🎉
32+
33+
## Schema
34+
35+
Create a `users` table with the following fields:
36+
37+
- `id` (primary key)
38+
- `created_at`
39+
- `confirmed_at` (can be `NULL`)
40+
- `first_name`
41+
- `last_name`
42+
- `email` (unique)
43+
44+
## Routes
45+
46+
- `/all-users` should respond with all users sorted by ID
47+
- `/unconfirmed-users` should respond with unconfirmed users
48+
- `/gmail-users` should respond with users with an @gmail.com email
49+
- `/2022-users` should respond with users created in 2022
50+
51+
---
52+
53+
## More routes
54+
55+
- `/user-count` should respond with the number of users
56+
- `/last-name-count` should respond with how many users there are with a given last name, sorted alphabetically
57+
- `/first-user` should respond with the first user. If there are no users in the table, respond with a 404
58+
59+
## Frontend <-> Backend integration
60+
61+
Turn the `/` route into a HTML page that fetches the count value from `/user-count` and shows it. Feel free to style this in whatever way you see fit.
62+
63+
Optional: update the count every 2 seconds or something like that.

0 commit comments

Comments
 (0)