Skip to content

Commit bbc2bcd

Browse files
committed
Replace Nodemon with node --watch
1 parent f72f7de commit bbc2bcd

File tree

7 files changed

+22
-96
lines changed

7 files changed

+22
-96
lines changed

src/content/3/en/part3a.md

Lines changed: 13 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -356,71 +356,39 @@ The experiment shown below illustrates this point:
356356

357357
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!
358358

359-
### nodemon
359+
### Automatic Change Tracking
360360

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.
362362

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:
394364

395365
```bash
396-
node_modules/.bin/nodemon index.js
366+
node --watch index.js
397367
```
398368

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).
400370

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:
402372

403373
```bash
404374
{
405375
// ..
406376
"scripts": {
407377
"start": "node index.js",
408-
"dev": "nodemon index.js", // highlight-line
378+
"dev": "node --watch index.js", // highlight-line
409379
"test": "echo \"Error: no test specified\" && exit 1"
410380
},
411381
// ..
412382
}
413383
```
414384

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
418386

419387
```bash
420388
npm run dev
421389
```
422390

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>.
424392

425393
### REST
426394

@@ -615,11 +583,11 @@ Before we implement the rest of the application logic, let's verify with Postman
615583

616584
The application prints the data that we sent in the request to the console:
617585

618-
![terminal printing content provided in postman](../../images/3/15new.png)
586+
![terminal printing content provided in postman](../../images/3/15c.png)
619587

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:
621589

622-
![nodemon error as typing requre not defined](../../images/3/16e.png)
590+
![console error about SyntaxError](../../images/3/16_25.png)
623591

624592
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.
625593

@@ -633,7 +601,7 @@ The <i>Content-Type</i> header is set to <i>text/plain</i>:
633601

634602
The server appears to only receive an empty object:
635603

636-
![nodemon output showing empty curly braces](../../images/3/19.png)
604+
![console output showing empty curly braces](../../images/3/19_25.png)
637605

638606
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>.
639607

src/content/3/en/part3c.md

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -41,12 +41,6 @@ Debugging is also possible with the Chrome developer console by starting your ap
4141
node --inspect index.js
4242
```
4343

44-
You can also pass the `--inspect` flag to `nodemon`:
45-
46-
```bash
47-
nodemon --inspect index.js
48-
```
49-
5044
You can access the debugger by clicking the green icon - the node logo - that appears in the Chrome developer console:
5145

5246
![dev tools with green node logo icon](../../images/3/37.png)

src/content/3/en/part3d.md

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -337,7 +337,6 @@ It is recommended to create a separate _npm script_ for linting:
337337
// ...
338338
"scripts": {
339339
"start": "node index.js",
340-
"dev": "nodemon index.js",
341340
// ...
342341
"lint": "eslint ." // highlight-line
343342
},

src/content/3/fi/osa3a.md

Lines changed: 9 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -353,67 +353,32 @@ Seuraava interaktiivisessa [node-repl](https://nodejs.org/docs/latest-v8.x/api/r
353353

354354
Saat käynnistettyä interaktiivisen node-repl:in kirjoittamalla komentoriville _node_. Komentojen toimivuutta on koodatessa kätevä kokeilla konsolissa, suosittelen!
355355

356-
### nodemon
356+
### Muutoksien automaattinen seuraaminen
357357

358358
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.
359359

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:
365361

366362
```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
382364
```
383365

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-
398366
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.
399367

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>:
402369

403370
```bash
404371
{
405372
// ..
406373
"scripts": {
407374
"start": "node index.js",
408-
"dev": "nodemon index.js", // highlight-line
375+
"dev": "node --watch index.js", // highlight-line
409376
"test": "echo \"Error: no test specified\" && exit 1"
410377
},
411378
// ..
412379
}
413380
```
414381

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-
417382
Voimme nyt käynnistää palvelimen sovelluskehitysmoodissa komennolla
418383

419384
```bash
@@ -605,11 +570,11 @@ Ennen toimintalogiikan viimeistelyä varmistetaan ensin Postmanilla, että lähe
605570

606571
Sovellus tulostaa lähetetyn vastaanottamansa datan terminaaliin:
607572

608-
![Konsoliin tulostuu palvelimen vastaanottama json-objekti](../../images/3/15new.png)
573+
![Konsoliin tulostuu palvelimen vastaanottama json-objekti](../../images/3/15c.png)
609574

610-
**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:
611576

612-
![konsoliin tulostuu epävalidista javascriptistä johtuva parse error ‑virheilmoitus](../../images/3/16e.png)
577+
![konsoliin tulostuu epävalidista javascriptistä johtuva parse error ‑virheilmoitus](../../images/3/16_25.png)
613578

614579
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.
615580

@@ -623,7 +588,7 @@ Headerin <i>Content-Type</i> arvoksi asettuu <i>text/plain</i>:
623588

624589
Palvelin näyttää vastaanottavan ainoastaan tyhjän olion:
625590

626-
![Konsoliin tulostuu tyhjä json](../../images/3/19.png)
591+
![Konsoliin tulostuu tyhjä json](../../images/3/19_25.png)
627592

628593
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).
629594

src/content/images/3/15c.png

6.12 KB
Loading

src/content/images/3/16_25.png

48.2 KB
Loading

src/content/images/3/19_25.png

5.72 KB
Loading

0 commit comments

Comments
 (0)