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
+13-45Lines changed: 13 additions & 45 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -356,71 +356,39 @@ The experiment shown below illustrates this point:
356
356
357
357
The experiment above was done in the interactive [node-repl](https://nodejs.org/docs/latest-v18.x/api/repl.html). You can start the interactive node-repl by typing in _node_ in the command line. The repl is particularly useful for testing how commands work while you're writing application code. I highly recommend this!
358
358
359
-
### nodemon
359
+
### Automatic Change Tracking
360
360
361
-
If we make changes to the application's code we have to restart the application to see the changes. We restart the application by first shutting it down by typing _Ctrl+C_ and then restarting the application. Compared to the convenient workflow in React where the browser automatically reloaded after changes were made, this feels slightly cumbersome.
361
+
If we change the application's code, we first need to stop the application from the console (_ctrl_ + _c_) and then restart it for the changes to take effect. Restarting feels cumbersome compared to React's smooth workflow, where the browser automatically updates when the code changes.
362
362
363
-
The solution to this problem is [nodemon](https://github.com/remy/nodemon):
364
-
365
-
> <i>nodemon will watch the files in the directory in which nodemon was started, and if any files change, nodemon will automatically restart your node application.</i>
366
-
367
-
Let's install nodemon by defining it as a <i>development dependency</i> with the command:
368
-
369
-
```bash
370
-
npm install --save-dev nodemon
371
-
```
372
-
373
-
The contents of <i>package.json</i> have also changed:
374
-
375
-
```json
376
-
{
377
-
//...
378
-
"dependencies": {
379
-
"express": "^4.21.2"
380
-
},
381
-
"devDependencies": {
382
-
"nodemon": "^3.1.9"
383
-
}
384
-
}
385
-
```
386
-
387
-
If you accidentally used the wrong command and the nodemon dependency was added under "dependencies" instead of "devDependencies", then manually change the contents of <i>package.json</i> to match what is shown above.
388
-
389
-
By development dependencies, we are referring to tools that are needed only during the development of the application, e.g. for testing or automatically restarting the application, like <i>nodemon</i>.
390
-
391
-
These development dependencies are not needed when the application is run in production mode on the production server (e.g. Fly.io or Heroku).
392
-
393
-
We can start our application with <i>nodemon</i> like this:
363
+
You can make the server track our changes by starting it with the _--watch_ option:
394
364
395
365
```bash
396
-
node_modules/.bin/nodemon index.js
366
+
node --watch index.js
397
367
```
398
368
399
-
Changes to the application code now cause the server to restart automatically. It's worth noting that even though the backend server restarts automatically, the browser still has to be manually refreshed. This is because unlike when working in React, we don't have the [hot reload](https://gaearon.github.io/react-hot-loader/getstarted/) functionality needed to automatically reload the browser.
369
+
Now, changes to the application's code will cause the server to restart automatically. Note that although the server restarts automatically, you still need to refresh the browser. Unlike with React, we do not have, nor could we have, a hot reload functionality that updates the browser in this scenario (where we return JSON data).
400
370
401
-
The command is long and quite unpleasant, so let's define a dedicated <i>npm script</i> for it in the <i>package.json</i> file:
371
+
Let's define a custom <i>npm script</i> in the <i>package.json</i> file to start the development server:
402
372
403
373
```bash
404
374
{
405
375
// ..
406
376
"scripts": {
407
377
"start": "node index.js",
408
-
"dev": "nodemon index.js", // highlight-line
378
+
"dev": "node --watch index.js", // highlight-line
409
379
"test": "echo \"Error: no test specified\" && exit 1"
410
380
},
411
381
// ..
412
382
}
413
383
```
414
384
415
-
In the script there is no need to specify the <i>node\_modules/.bin/nodemon</i> path to nodemon, because _npm_ automatically knows to search for the file from that directory.
416
-
417
-
We can now start the server in development mode with the command:
385
+
We can now start the server in development mode with the command
418
386
419
387
```bash
420
388
npm run dev
421
389
```
422
390
423
-
Unlike with the <i>start</i> and <i>test</i> scripts, we also have to add <i>run</i> to the command because it is a non-native script.
391
+
Unlike when running the <i>start</i> or <i>test</i> scripts, the command must include <i>run</i>.
424
392
425
393
### REST
426
394
@@ -615,11 +583,11 @@ Before we implement the rest of the application logic, let's verify with Postman
615
583
616
584
The application prints the data that we sent in the request to the console:
617
585
618
-

586
+

619
587
620
-
**NB** <i>Keep the terminal running the application visible at all times</i> when you are working on the backend. Thanks to Nodemon any changes we make to the code will restart the application. If you pay attention to the console, you will immediately be able to pick up on errors that occur in the application:
588
+
**NOTE:**When programming the backend, <i>keep the console running the application visible at all times</i>. The development server will restart if changes are made to the code, so by monitoring the console, you will immediately notice if there is an error in the application's code:
621
589
622
-

590
+

623
591
624
592
Similarly, it is useful to check the console to make sure that the backend behaves as we expect it to in different situations, like when we send data with an HTTP POST request. Naturally, it's a good idea to add lots of <em>console.log</em> commands to the code while the application is still being developed.
625
593
@@ -633,7 +601,7 @@ The <i>Content-Type</i> header is set to <i>text/plain</i>:
633
601
634
602
The server appears to only receive an empty object:
The server will not be able to parse the data correctly without the correct value in the header. It won't even try to guess the format of the data since there's a [massive amount](https://developer.mozilla.org/en-US/docs/Web/HTTP/Basics_of_HTTP/MIME_types) of potential <i>Content-Types</i>.
Copy file name to clipboardExpand all lines: src/content/3/fi/osa3a.md
+9-44Lines changed: 9 additions & 44 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -353,67 +353,32 @@ Seuraava interaktiivisessa [node-repl](https://nodejs.org/docs/latest-v8.x/api/r
353
353
354
354
Saat käynnistettyä interaktiivisen node-repl:in kirjoittamalla komentoriville _node_. Komentojen toimivuutta on koodatessa kätevä kokeilla konsolissa, suosittelen!
355
355
356
-
### nodemon
356
+
### Muutoksien automaattinen seuraaminen
357
357
358
358
Jos muutamme sovelluksen koodia, joudumme ensin sulkemaan sovelluksen konsolista (_ctrl_ + _c_) ja sitten käynnistämään sovelluksen uudelleen, jotta muutokset tulevat voimaan. Uudelleenkäynnistely tuntuu kömpelöltä verrattuna Reactin mukavaan workflow'hun, jossa selain päivittyi automaattisesti koodin muuttuessa.
359
359
360
-
Ongelmaan on ratkaisu nimeltä [nodemon](https://github.com/remy/nodemon):
361
-
362
-
> <i>nodemon will watch the files in the directory in which nodemon was started, and if any files change, nodemon will automatically restart your node application.</i>
363
-
364
-
Asennetaan nodemon määrittelemällä se <i>kehitysaikaiseksi riippuvuudeksi</i> (development dependency) komennolla:
360
+
Palvelimen saa seuraamaan tekemiämme muutoksia, kun sen käynnistää käyttäen _--watch_-optiota:
365
361
366
362
```bash
367
-
npm install --save-dev nodemon
368
-
```
369
-
370
-
Tiedoston <i>package.json</i> sisältö muuttuu seuraavasti:
371
-
372
-
```json
373
-
{
374
-
//...
375
-
"dependencies": {
376
-
"express": "^4.21.2"
377
-
},
378
-
"devDependencies": {
379
-
"nodemon": "^3.1.9"
380
-
}
381
-
}
363
+
node --watch index.js
382
364
```
383
365
384
-
Jos nodemon-riippuvuus kuitenkin meni sovelluksessasi normaaliin "dependencies"-ryhmään, päivitä <i>package.json</i> manuaalisesti vastaamaan yllä näkyvää (kuitenkin versiot säilyttäen).
385
-
386
-
Kehitysaikaisilla riippuvuuksilla tarkoitetaan työkaluja, joita tarvitaan ainoastaan sovellusta kehitettäessä esim. testaukseen tai sovelluksen automaattiseen uudelleenkäynnistykseen kuten <i>nodemon</i>.
387
-
388
-
Kun sovellusta suoritetaan tuotantomoodissa eli samoin kuin sitä tullaan suorittamaan tuotantopalvelimella (esim. Fly.io:ssa, johon tulemme kohta siirtämään sovelluksemme), ei kehitysaikaisia riippuvuuksia tarvita.
389
-
390
-
Voimme käynnistää ohjelman <i>nodemonilla</i> seuraavasti:
391
-
392
-
```bash
393
-
node_modules/.bin/nodemon index.js
394
-
```
395
-
396
-
Huom: komennon tämä muoto ei välttämättä toimi Windowsilla. Se ei kuitenkaan haittaa sillä 5 sentin päästä kerrotaan komennosta parempi muoto.
397
-
398
366
Sovelluksen koodin muutokset aiheuttavat nyt automaattisen palvelimen uudelleenkäynnistymisen. Kannattaa huomata, että vaikka palvelin uudelleenkäynnistyy automaattisesti, selain täytyy kuitenkin refreshata, sillä toisin kuin Reactin yhteydessä, meillä ei nyt ole eikä tässä skenaariossa (jossa palautamme JSON-muotoista dataa) edes voisikaan olla selainta päivittävää [hot reload](https://gaearon.github.io/react-hot-loader/getstarted/) ‑toiminnallisuutta.
399
367
400
-
401
-
Komento on ikävä, joten määritellään sitä varten <i>npm-skripti</i> tiedostoon <i>package.json</i>:
368
+
Määritellään kehityspalvelimen käynnistämistä varten oma <i>npm-skripti</i> tiedostoon <i>package.json</i>:
402
369
403
370
```bash
404
371
{
405
372
// ..
406
373
"scripts": {
407
374
"start": "node index.js",
408
-
"dev": "nodemon index.js", // highlight-line
375
+
"dev": "node --watch index.js", // highlight-line
409
376
"test": "echo \"Error: no test specified\" && exit 1"
410
377
},
411
378
// ..
412
379
}
413
380
```
414
381
415
-
Skriptissä ei ole tarvetta käyttää nodemonin polusta sen täydellistä muotoa <i>node\_modules/.bin/nodemon</i> sillä _npm_ osaa etsiä automaattisesti suoritettavaa tiedostoa kyseisestä hakemistosta.
416
-
417
382
Voimme nyt käynnistää palvelimen sovelluskehitysmoodissa komennolla
418
383
419
384
```bash
@@ -605,11 +570,11 @@ Ennen toimintalogiikan viimeistelyä varmistetaan ensin Postmanilla, että lähe
**HUOM:** Kun ohjelmoit backendia, <i>pidä sovellusta suorittava konsoli koko ajan näkyvillä</i>. Nodemonin ansiosta sovellus käynnistyy uudelleen jos koodiin tehdään muutoksia. Jos seuraat konsolia, huomaat välittömästi jos sovelluksen koodiin tulee virhe:
575
+
**HUOM:** Kun ohjelmoit backendia, <i>pidä sovellusta suorittava konsoli koko ajan näkyvillä</i>. Kehityspalvelin käynnistyy uudelleen, jos koodiin tehdään muutoksia, joten jos seuraat konsolia, huomaat välittömästi jos sovelluksen koodiin tulee virhe:
Konsolista kannattaa seurata myös, reagoiko backend odotetulla tavalla esim. kun sovellukselle lähetetään dataa metodilla HTTP POST. Backendiin kannattaa luonnollisesti lisäillä runsaat määrät <em>console.log</em>-komentoja kun sovellus on kehitysvaiheessa.
Ilman oikeaa headerin arvoa palvelin ei osaa parsia dataa oikeaan muotoon. Se ei edes yritä arvailla missä muodossa data on, sillä potentiaalisia datan siirtomuotoja eli <i>Content-Typejä</i> on olemassa [suuri määrä](https://developer.mozilla.org/en-US/docs/Web/HTTP/Basics_of_HTTP/MIME_types).
0 commit comments