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
Defining Node [modules](https://nodejs.org/docs/latest-v18.x/api/modules.html) differs slightly from the way of defining [ES6 modules](/en/part2/rendering_a_collection_modules#refactoring-modules) in part 2.
458
+
There are some changes in the code compared to before. The database connection URL is now passed to the application via the MONGODB_URI environment variable, as hardcoding it into the application is not a good idea:
460
459
461
-
The public interface of the module is defined by setting a value to the _module.exports_ variable. We will set the value to be the <i>Note</i> model. The other things defined inside of the module, like the variables _mongoose_ and _url_ will not be accessible or visible to users of the module.
460
+
```js
461
+
consturl=process.env.MONGODB_URI
462
+
```
462
463
463
-
Importing the module happens by adding the following line to <i>index.js</i>:
464
+
There are many ways to define the value of an environment variable. For example, we can define it when starting the application as follows:
464
465
465
-
```js
466
-
constNote=require('./models/note')
466
+
```bash
467
+
MONGODB_URI="your_connection_string_here" npm run dev
467
468
```
468
469
469
-
This way the _Note_ variable will be assigned to the same object that the module defines.
470
+
We will soon learn a more sophisticated way to define environment variables.
470
471
471
472
The way that the connection is made has changed slightly:
472
473
@@ -484,21 +485,22 @@ The method for establishing the connection is now given functions for dealing wi
484
485
485
486

486
487
487
-
It's also not a good idea to hardcode the address of the database into the code, so the url is obtained differently: the address of the database is passed to the application via the <em>MONGODB_URI</em> environment variable:
488
488
489
-
```js
490
-
consturl=process.env.MONGODB_URI
489
+
Defining Node [modules](https://nodejs.org/docs/latest-v18.x/api/modules.html) differs slightly from the way of defining [ES6 modules](/en/part2/rendering_a_collection_modules#refactoring-modules) in part 2.
491
490
492
-
console.log('connecting to', url)
493
-
```
491
+
The public interface of the module is defined by setting a value to the _module.exports_ variable. We will set the value to be the <i>Note</i> model. The other things defined inside of the module, like the variables _mongoose_ and _url_ will not be accessible or visible to users of the module.
494
492
495
-
There are many ways to define the value of an environment variable. One way would be to define it when the application is started:
493
+
Importing the module happens by adding the following line to <i>index.js</i>:
496
494
497
-
```bash
498
-
MONGODB_URI=address_here npm run dev
495
+
```js
496
+
constNote=require('./models/note')
499
497
```
500
498
501
-
A more sophisticated way is to use the [dotenv](https://github.com/motdotla/dotenv#readme) library. You can install the library with the command:
499
+
This way the _Note_ variable will be assigned to the same object that the module defines.
500
+
501
+
### Defining environment variables using the dotenv library
502
+
503
+
A more sophisticated way to define environment variables is to use the [dotenv](https://github.com/motdotla/dotenv#readme) library. You can install the library with the command:
502
504
503
505
```bash
504
506
npm install dotenv
@@ -524,7 +526,7 @@ Let's load the environment variables at the beginning of the index.js file so th
It's important that <i>dotenv</i> gets imported before the <i>note</i> model is imported. This ensures that the environment variables from the <i>.env</i> file are available globally before the code from the other modules is imported.
539
541
540
-
### Important note about environment variables
542
+
####Important note about defining environment variables in Fly.io and Render
541
543
542
-
Because GitHub is not used with Fly.io, the file .env also gets to the Fly.io servers when the app is deployed. Because of this, the env variables defined in the file will be available there.
544
+
**Fly.io users:**Because GitHub is not used with Fly.io, the file .env also gets to the Fly.io servers when the app is deployed. Because of this, the env variables defined in the file will be available there.
543
545
544
546
However, a [better option](https://community.fly.io/t/clarification-on-environment-variables/6309) is to prevent .env from being copied to Fly.io by creating in the project root the file _.dockerignore_, with the following contents
545
547
@@ -553,7 +555,7 @@ and set the env value from the command line with the command:
553
555
fly secrets set MONGODB_URI="mongodb+srv://fullstack:[email protected]/noteApp?retryWrites=true&w=majority&appName=Cluster0"
554
556
```
555
557
556
-
When using Render, the database url is given by defining the proper env in the dashboard:
558
+
**Render users:**When using Render, the database url is given by defining the proper env in the dashboard:
Noden [moduulien](https://nodejs.org/docs/latest-v8.x/api/modules.html) määrittely poikkeaa hiukan osassa 2 määrittelemistämme frontendin käyttämistä [ES6-moduuleista](/osa2/kokoelmien_renderointi_ja_moduulit#refaktorointia-moduulit).
452
+
Koodissa on jonkin verran muutoksia aiempaan. Tietokannan yhteysosoite välitetään sovellukselle nyt MONGODB_URI ympäristömuuttujan kautta, koska sen kovakoodaaminen sovellukseen ei ole järkevää:
453
453
454
-
Moduulin ulos näkyvä osa määritellään asettamalla arvo muuttujalle _module.exports_. Asetamme arvoksi modelin <i>Note</i>. Muut moduulin sisällä määritellyt asiat, esim. muuttujat _mongoose_ ja _url_ eivät näy moduulin käyttäjälle.
454
+
```js
455
+
consturl=process.env.MONGODB_URI
456
+
```
455
457
456
-
Moduulin käyttöönotto tapahtuu lisäämällä tiedostoon <i>index.js</i> seuraava rivi:
458
+
On useita tapoja määritellä ympäristömuuttujan arvo. Voimme esim. antaa sen ohjelman käynnistyksen yhteydessä seuraavasti:
457
459
458
-
```js
459
-
constNote=require('./models/note')
460
+
```bash
461
+
MONGODB_URI="osoite_tahan" npm run dev
460
462
```
461
463
462
-
Näin muuttuja _Note_ saa arvokseen saman olion, jonka moduuli määrittelee.
464
+
Opettelemme pian kehittyneemmän tavan määritellä ympäristömuuttujia.
463
465
464
466
Yhteyden muodostustavassa on pieni muutos aiempaan:
465
467
@@ -477,27 +479,27 @@ Yhteyden muodostavalle metodille on nyt rekisteröity onnistuneen ja epäonnistu
477
479
478
480

479
481
480
-
Ei ole myöskään hyvä idea kovakoodata tietokannan osoitetta koodiin, joten tietokannan osoite välitetään sovellukselle <em>MONGODB_URI</em> ympäristömuuttujan kautta:
482
+
Noden [moduulien](https://nodejs.org/docs/latest-v8.x/api/modules.html) määrittely poikkeaa hiukan osassa 2 määrittelemistämme frontendin käyttämistä [ES6-moduuleista](/osa2/kokoelmien_renderointi_ja_moduulit#refaktorointia-moduulit).
483
+
484
+
Moduulin ulos näkyvä osa määritellään asettamalla arvo muuttujalle _module.exports_. Asetamme arvoksi modelin <i>Note</i>. Muut moduulin sisällä määritellyt asiat, esim. muuttujat _mongoose_ ja _url_ eivät näy moduulin käyttäjälle.
481
485
482
-
```js
483
-
consturl=process.env.MONGODB_URI
486
+
Moduulin käyttöönotto tapahtuu lisäämällä tiedostoon <i>index.js</i> seuraava rivi:
484
487
485
-
console.log('connecting to', url)
488
+
```js
489
+
constNote=require('./models/note')
486
490
```
487
491
488
-
On useita tapoja määritellä ympäristömuuttujan arvo. Voimme esim. antaa sen ohjelman käynnistyksen yhteydessä seuraavasti:
492
+
Näin muuttuja _Note_ saa arvokseen saman olion, jonka moduuli määrittelee.
489
493
490
-
```bash
491
-
MONGODB_URI=osoite_tahan npm run dev
492
-
```
494
+
### Ympäristömuuttujien määritteleminen käyttäen dotenv-kirjastoa
493
495
494
-
Eräs kehittyneempi tapa on käyttää [dotenv](https://github.com/motdotla/dotenv#readme)-kirjastoa. Asennetaan kirjasto komennolla
496
+
Eräs kehittyneempi tapa ympäristömuuttujien määrittelemiseen on käyttää [dotenv](https://github.com/motdotla/dotenv#readme)-kirjastoa. Asennetaan kirjasto komennolla
495
497
496
498
```bash
497
499
npm install dotenv
498
500
```
499
501
500
-
Sovelluksen juurihakemistoon tehdään sitten tiedosto nimeltään <i>.env</i>, jonne tarvittavien ympäristömuuttujien arvot määritellään. Tiedosto näyttää seuraavalta:
502
+
Luodaan sitten sovelluksen juurihakemistoon tiedosto nimeltään <i>.env</i>, jonne tarvittavien ympäristömuuttujien arvot määritellään. Tiedosto näyttää seuraavalta:
On tärkeää, että <i>dotenv</i> otetaan käyttöön ennen modelin <i>note</i> importtaamista. Tällöin varmistutaan siitä, että tiedostossa <i>.env</i> olevat ympäristömuuttujat ovat alustettuja kun moduulin koodia importoidaan.
534
536
535
-
### Tärkeä huomio ympäristömuuttujista
537
+
####Tärkeä huomio ympäristömuuttujien määrittelemisestä Fly.io:ssa ja Renderissä
536
538
537
-
Koska Fly.io ei hyödynnä gitiä, menee myös .env-tiedosto Fly.io:n palvelimelle, ja ympäristömuuttujien arvo välittyy myös sinne.
539
+
**Fly.io:n käyttäjät:**Koska Fly.io ei hyödynnä gitiä, menee myös .env-tiedosto Fly.io:n palvelimelle, ja ympäristömuuttujien arvo välittyy myös sinne.
538
540
539
541
[Tietoturvallisempi vaihtoehto](https://community.fly.io/t/clarification-on-environment-variables/6309) on kuitenkin estää tiedoston .env siirtyminen Fly.io:n tekemällä hakemiston juureen tiedosto _.dockerignore_, jolla on sisältö
540
542
@@ -548,7 +550,7 @@ ja asettaa ympäristömuuttujan arvo komennolla:
548
550
fly secrets set MONGODB_URI='mongodb+srv://fullstack:[email protected]/noteApp?retryWrites=true&w=majority&appName=Cluster0'
549
551
```
550
552
551
-
Renderiä käytettäessä tietokannan osoitteen kertova ympäristömuuttuja määritellään dashboardista käsin:
553
+
**Renderin käyttäjät:**Renderiä käytettäessä tietokannan osoitteen kertova ympäristömuuttuja määritellään dashboardista käsin:
0 commit comments