Skip to content

Commit 8e34715

Browse files
committed
part 3
1 parent 4b4629d commit 8e34715

File tree

9 files changed

+125
-95
lines changed

9 files changed

+125
-95
lines changed

src/content/3/en/part3a.md

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -226,9 +226,9 @@ When we open the browser, the displayed format is exactly the same as in [part 2
226226

227227
Implementing our server code directly with Node's built-in [http](https://nodejs.org/docs/latest-v18.x/api/http.html) web server is possible. However, it is cumbersome, especially once the application grows in size.
228228

229-
Many libraries have been developed to ease server-side development with Node, by offering a more pleasing interface to work with the built-in http module. These libraries aim to provide a better abstraction for general use cases we usually require to build a backend server. By far the most popular library intended for this purpose is [express](http://expressjs.com).
229+
Many libraries have been developed to ease server-side development with Node, by offering a more pleasing interface to work with the built-in http module. These libraries aim to provide a better abstraction for general use cases we usually require to build a backend server. By far the most popular library intended for this purpose is [Express](http://expressjs.com).
230230

231-
Let's take express into use by defining it as a project dependency with the command:
231+
Let's take Express into use by defining it as a project dependency with the command:
232232

233233
```bash
234234
npm install express
@@ -245,11 +245,11 @@ The dependency is also added to our <i>package.json</i> file:
245245
}
246246
```
247247

248-
The source code for the dependency is installed in the <i>node\_modules</i> directory located at the root of the project. In addition to express, you can find a great number of other dependencies in the directory:
248+
The source code for the dependency is installed in the <i>node\_modules</i> directory located at the root of the project. In addition to Express, you can find a great number of other dependencies in the directory:
249249

250250
![ls command listing of dependencies in directory](../../images/3/4.png)
251251

252-
These are the dependencies of the express library and the dependencies of all of its dependencies, and so forth. These are called the [transitive dependencies](https://lexi-lambda.github.io/blog/2016/08/24/understanding-the-npm-dependency-model/) of our project.
252+
These are the dependencies of the Express library and the dependencies of all of its dependencies, and so forth. These are called the [transitive dependencies](https://lexi-lambda.github.io/blog/2016/08/24/understanding-the-npm-dependency-model/) of our project.
253253

254254
Version 4.18.2 of Express was installed in our project. What does the caret in front of the version number in <i>package.json</i> mean?
255255

@@ -259,7 +259,7 @@ Version 4.18.2 of Express was installed in our project. What does the caret in f
259259

260260
The versioning model used in npm is called [semantic versioning](https://docs.npmjs.com/about-semantic-versioning).
261261

262-
The caret in the front of <i>^4.18.2</i> means that if and when the dependencies of a project are updated, the version of express that is installed will be at least <i>4.18.2</i>. However, the installed version of express can also have a larger <i>patch</i> number (the last number), or a larger <i>minor</i> number (the middle number). The major version of the library indicated by the first <i>major</i> number must be the same.
262+
The caret in the front of <i>^4.18.2</i> means that if and when the dependencies of a project are updated, the version of Express that is installed will be at least <i>4.18.2</i>. However, the installed version of Express can also have a larger <i>patch</i> number (the last number), or a larger <i>minor</i> number (the middle number). The major version of the library indicated by the first <i>major</i> number must be the same.
263263

264264
We can update the dependencies of the project with the command:
265265

@@ -275,7 +275,7 @@ npm install
275275

276276
If the <i>major</i> number of a dependency does not change, then the newer versions should be [backwards compatible](https://en.wikipedia.org/wiki/Backward_compatibility). This means that if our application happened to use version 4.99.175 of Express in the future, then all the code implemented in this part would still have to work without making changes to the code. In contrast, the future 5.0.0 version of Express [may contain](https://expressjs.com/en/guide/migrating-5.html) changes that would cause our application to no longer work.
277277

278-
### Web and express
278+
### Web and Express
279279

280280
Let's get back to our application and make the following changes:
281281

@@ -303,7 +303,7 @@ app.listen(PORT, () => {
303303

304304
To get the new version of our application into use, first we have to restart it.
305305

306-
The application did not change a whole lot. Right at the beginning of our code, we're importing _express_, which this time is a <i>function</i> that is used to create an express application stored in the _app_ variable:
306+
The application did not change a whole lot. Right at the beginning of our code, we're importing _express_, which this time is a <i>function</i> that is used to create an Express application stored in the _app_ variable:
307307

308308
```js
309309
const express = require('express')
@@ -320,7 +320,7 @@ app.get('/', (request, response) => {
320320

321321
The event handler function accepts two parameters. The first [request](http://expressjs.com/en/4x/api.html#req) parameter contains all of the information of the HTTP request, and the second [response](http://expressjs.com/en/4x/api.html#res) parameter is used to define how the request is responded to.
322322

323-
In our code, the request is answered by using the [send](http://expressjs.com/en/4x/api.html#res.send) method of the _response_ object. Calling the method makes the server respond to the HTTP request by sending a response containing the string <code>\<h1>Hello World!\</h1></code> that was passed to the _send_ method. Since the parameter is a string, express automatically sets the value of the <i>Content-Type</i> header to be <i>text/html</i>. The status code of the response defaults to 200.
323+
In our code, the request is answered by using the [send](http://expressjs.com/en/4x/api.html#res.send) method of the _response_ object. Calling the method makes the server respond to the HTTP request by sending a response containing the string <code>\<h1>Hello World!\</h1></code> that was passed to the _send_ method. Since the parameter is a string, Express automatically sets the value of the <i>Content-Type</i> header to be <i>text/html</i>. The status code of the response defaults to 200.
324324

325325
We can verify this from the <i>Network</i> tab in developer tools:
326326

@@ -346,7 +346,7 @@ In the earlier version where we were only using Node, we had to transform the da
346346
response.end(JSON.stringify(notes))
347347
```
348348

349-
With express, this is no longer required, because this transformation happens automatically.
349+
With Express, this is no longer required, because this transformation happens automatically.
350350

351351
It's worth noting that [JSON](https://en.wikipedia.org/wiki/JSON) is a string and not a JavaScript object like the value assigned to _notes_.
352352

@@ -464,7 +464,7 @@ Let's expand our application so that it offers a REST interface for operating on
464464

465465
The unique address we will use for an individual note is of the form <i>notes/10</i>, where the number at the end refers to the note's unique id number.
466466

467-
We can define [parameters](http://expressjs.com/en/guide/routing.html#route-parameters) for routes in express by using the colon syntax:
467+
We can define [parameters](http://expressjs.com/en/guide/routing.html#route-parameters) for routes in Express by using the colon syntax:
468468

469469
```js
470470
app.get('/api/notes/:id', (request, response) => {
@@ -634,7 +634,7 @@ If you use *IntelliJ WebStorm* instead, you can use a similar procedure with its
634634

635635
Next, let's make it possible to add new notes to the server. Adding a note happens by making an HTTP POST request to the address <http://localhost:3001/api/notes>, and by sending all the information for the new note in the request [body](https://www.rfc-editor.org/rfc/rfc9112#name-message-body) in JSON format.
636636

637-
To access the data easily, we need the help of the express [json-parser](https://expressjs.com/en/api.html) that we can use with the command _app.use(express.json())_.
637+
To access the data easily, we need the help of the Express [json-parser](https://expressjs.com/en/api.html) that we can use with the command _app.use(express.json())_.
638638

639639
Let's activate the json-parser and implement an initial handler for dealing with the HTTP POST requests:
640640

@@ -950,7 +950,7 @@ POST is the only HTTP request type that is neither <i>safe</i> nor <i>idempotent
950950

951951
### Middleware
952952

953-
The express [json-parser](https://expressjs.com/en/api.html) used earlier is a [middleware](http://expressjs.com/en/guide/using-middleware.html).
953+
The Express [json-parser](https://expressjs.com/en/api.html) used earlier is a [middleware](http://expressjs.com/en/guide/using-middleware.html).
954954

955955
Middleware are functions that can be used for handling _request_ and _response_ objects.
956956

src/content/3/en/part3b.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -282,15 +282,15 @@ The backend directory should now look as follows:
282282
283283
![bash screenshot of ls showing dist directory](../../images/3/27v.png)
284284
285-
To make express show <i>static content</i>, the page <i>index.html</i> and the JavaScript, etc., it fetches, we need a built-in middleware from Express called [static](http://expressjs.com/en/starter/static-files.html).
285+
To make Express show <i>static content</i>, the page <i>index.html</i> and the JavaScript, etc., it fetches, we need a built-in middleware from Express called [static](http://expressjs.com/en/starter/static-files.html).
286286
287287
When we add the following amidst the declarations of middlewares
288288
289289
```js
290290
app.use(express.static('dist'))
291291
```
292292
293-
whenever express gets an HTTP GET request it will first check if the <i>dist</i> directory contains a file corresponding to the request's address. If a correct file is found, express will return it.
293+
whenever Express gets an HTTP GET request it will first check if the <i>dist</i> directory contains a file corresponding to the request's address. If a correct file is found, Express will return it.
294294
295295
Now HTTP GET requests to the address <i>www.serversaddress.com/index.html</i> or <i>www.serversaddress.com</i> will show the React frontend. GET requests to the address <i>www.serversaddress.com/api/notes</i> will be handled by the backend code.
296296

src/content/3/en/part3c.md

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ When bugs occur, <i>the worst of all possible strategies</i> is to continue writ
7171

7272
### MongoDB
7373

74-
To store our saved notes indefinitely, we need a database. Most of the courses taught at the University of Helsinki use relational databases. In most parts of this course, we will use [MongoDB](https://www.mongodb.com/) which is [document database](https://en.wikipedia.org/wiki/Document-oriented_database).
74+
To store our saved notes indefinitely, we need a database. Most of the courses taught at the University of Helsinki use relational databases. In most parts of this course, we will use [MongoDB](https://www.mongodb.com/) which is a [document database](https://en.wikipedia.org/wiki/Document-oriented_database).
7575

7676
The reason for using Mongo as the database is its lower complexity compared to a relational database. [Part 13](/en/part13) of the course shows how to build Node.js backends that use a relational database.
7777

@@ -437,7 +437,6 @@ The code automatically uses the defined _toJSON_ when formatting notes to the re
437437

438438
### Moving db configuration to its own module
439439

440-
441440
Before we refactor the rest of the backend to use the database, let's extract the Mongoose-specific code into its own module.
442441

443442
Let's create a new directory for the module called <i>models</i>, and add a file called <i>note.js</i>:
@@ -578,7 +577,7 @@ When using Render, the database url is given by defining the proper env in the d
578577

579578
![browser showing render environment variables](../../images/3/render-env.png)
580579

581-
Set just the URL starting with <i>mongodb+srv://</i> to the _value_ field.
580+
Set just the URL starting with <i>mongodb+srv://...</i> to the _value_ field.
582581

583582
### Using database in route handlers
584583

@@ -796,7 +795,7 @@ Note that the error-handling middleware has to be the last loaded middleware, al
796795

797796
### The order of middleware loading
798797

799-
The execution order of middleware is the same as the order that they are loaded into express with the _app.use_ function. For this reason, it is important to be careful when defining middleware.
798+
The execution order of middleware is the same as the order that they are loaded into Express with the _app.use_ function. For this reason, it is important to be careful when defining middleware.
800799

801800
The correct order is the following:
802801

src/content/3/en/part3d.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -230,7 +230,7 @@ We will answer all of the questions:
230230

231231
![terminal output from ESlint init](../../images/3/52new.png)
232232

233-
The configuration will be saved in the _.eslintrc.js_ file. We will change _browser_ to _node_ in the _env_ configuration:
233+
The configuration will be saved in the _.eslintrc.js_ file. We will change _browser_ to _node_ in the _env_ configuration:
234234

235235
```js
236236
module.exports = {
@@ -266,7 +266,7 @@ Let's change the configuration a bit. Install a [plugin](https://eslint.style/pa
266266
npm install --save-dev @stylistic/eslint-plugin-js
267267
```
268268

269-
Enable the plugin and add an extends definiton and four code style rules:
269+
Enable the plugin and add an "extends" definition and four code style rules:
270270

271271
```js
272272
module.exports = {

src/content/3/es/part3.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ lang: es
88

99
En esta parte, nuestro enfoque se desplaza hacia el backend, es decir, hacia la implementación de la funcionalidad en el lado del servidor. Implementaremos una API REST simple en Node.js utilizando la librería Express, y los datos de la aplicación se almacenarán en una base de datos MongoDB. Al final de esta parte, desplegaremos nuestra aplicación en Internet.
1010

11-
<i>Parte actualizada el 14 de Agosto de 2023</i>
12-
- <i>Create React App reemplazado con Vite</i>
11+
<i>Parte actualizada el 9 de Febrero de 2024</i>
12+
- <i>No hay cambios importantes</i>
1313

1414
</div>

0 commit comments

Comments
 (0)