You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: src/content/3/en/part3a.md
+12-12Lines changed: 12 additions & 12 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -226,9 +226,9 @@ When we open the browser, the displayed format is exactly the same as in [part 2
226
226
227
227
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.
228
228
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).
230
230
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:
232
232
233
233
```bash
234
234
npm install express
@@ -245,11 +245,11 @@ The dependency is also added to our <i>package.json</i> file:
245
245
}
246
246
```
247
247
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:
249
249
250
250

251
251
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.
253
253
254
254
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?
255
255
@@ -259,7 +259,7 @@ Version 4.18.2 of Express was installed in our project. What does the caret in f
259
259
260
260
The versioning model used in npm is called [semantic versioning](https://docs.npmjs.com/about-semantic-versioning).
261
261
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.
263
263
264
264
We can update the dependencies of the project with the command:
265
265
@@ -275,7 +275,7 @@ npm install
275
275
276
276
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.
277
277
278
-
### Web and express
278
+
### Web and Express
279
279
280
280
Let's get back to our application and make the following changes:
281
281
@@ -303,7 +303,7 @@ app.listen(PORT, () => {
303
303
304
304
To get the new version of our application into use, first we have to restart it.
305
305
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:
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.
322
322
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.
324
324
325
325
We can verify this from the <i>Network</i> tab in developer tools:
326
326
@@ -346,7 +346,7 @@ In the earlier version where we were only using Node, we had to transform the da
346
346
response.end(JSON.stringify(notes))
347
347
```
348
348
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.
350
350
351
351
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_.
352
352
@@ -464,7 +464,7 @@ Let's expand our application so that it offers a REST interface for operating on
464
464
465
465
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.
466
466
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:
@@ -634,7 +634,7 @@ If you use *IntelliJ WebStorm* instead, you can use a similar procedure with its
634
634
635
635
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.
636
636
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())_.
638
638
639
639
Let's activate the json-parser and implement an initial handler for dealing with the HTTP POST requests:
640
640
@@ -950,7 +950,7 @@ POST is the only HTTP request type that is neither <i>safe</i> nor <i>idempotent
950
950
951
951
### Middleware
952
952
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).
954
954
955
955
Middleware are functions that can be used for handling _request_ and _response_ objects.
Copy file name to clipboardExpand all lines: src/content/3/en/part3b.md
+2-2Lines changed: 2 additions & 2 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -282,15 +282,15 @@ The backend directory should now look as follows:
282
282
283
283

284
284
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).
286
286
287
287
When we add the following amidst the declarations of middlewares
288
288
289
289
```js
290
290
app.use(express.static('dist'))
291
291
```
292
292
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.
294
294
295
295
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.
Copy file name to clipboardExpand all lines: src/content/3/en/part3c.md
+3-4Lines changed: 3 additions & 4 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -71,7 +71,7 @@ When bugs occur, <i>the worst of all possible strategies</i> is to continue writ
71
71
72
72
### MongoDB
73
73
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).
75
75
76
76
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.
77
77
@@ -437,7 +437,6 @@ The code automatically uses the defined _toJSON_ when formatting notes to the re
437
437
438
438
### Moving db configuration to its own module
439
439
440
-
441
440
Before we refactor the rest of the backend to use the database, let's extract the Mongoose-specific code into its own module.
442
441
443
442
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
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.
582
581
583
582
### Using database in route handlers
584
583
@@ -796,7 +795,7 @@ Note that the error-handling middleware has to be the last loaded middleware, al
796
795
797
796
### The order of middleware loading
798
797
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.
Copy file name to clipboardExpand all lines: src/content/3/es/part3.md
+2-2Lines changed: 2 additions & 2 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -8,7 +8,7 @@ lang: es
8
8
9
9
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.
0 commit comments