Skip to content

Commit ba88057

Browse files
committed
Re-arrange things in db module section
1 parent 448d512 commit ba88057

File tree

2 files changed

+46
-42
lines changed

2 files changed

+46
-42
lines changed

src/content/3/en/part3c.md

Lines changed: 23 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -428,8 +428,7 @@ mongoose.set('strictQuery', false)
428428

429429
const url = process.env.MONGODB_URI // highlight-line
430430

431-
console.log('connecting to', url) // highlight-line
432-
431+
console.log('connecting to', url)
433432
mongoose.connect(url)
434433
// highlight-start
435434
.then(result => {
@@ -456,17 +455,19 @@ noteSchema.set('toJSON', {
456455
module.exports = mongoose.model('Note', noteSchema) // highlight-line
457456
```
458457

459-
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:
460459

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+
const url = process.env.MONGODB_URI
462+
```
462463

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

465-
```js
466-
const Note = require('./models/note')
466+
```bash
467+
MONGODB_URI="your_connection_string_here" npm run dev
467468
```
468469

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

471472
The way that the connection is made has changed slightly:
472473

@@ -484,21 +485,22 @@ The method for establishing the connection is now given functions for dealing wi
484485

485486
![node output when wrong username/password](../../images/3/45e.png)
486487

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

489-
```js
490-
const url = 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.
491490

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

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

497-
```bash
498-
MONGODB_URI=address_here npm run dev
495+
```js
496+
const Note = require('./models/note')
499497
```
500498

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

503505
```bash
504506
npm install dotenv
@@ -524,7 +526,7 @@ Let's load the environment variables at the beginning of the index.js file so th
524526
```js
525527
require('dotenv').config() // highlight-line
526528
const express = require('express')
527-
const Note = require('./models/note')
529+
const Note = require('./models/note') // highlight-line
528530

529531
const app = express()
530532
// ..
@@ -537,9 +539,9 @@ app.listen(PORT, () => {
537539

538540
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.
539541

540-
### Important note about environment variables
542+
#### Important note about defining environment variables in Fly.io and Render
541543

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

544546
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
545547

@@ -553,7 +555,7 @@ and set the env value from the command line with the command:
553555
fly secrets set MONGODB_URI="mongodb+srv://fullstack:[email protected]/noteApp?retryWrites=true&w=majority&appName=Cluster0"
554556
```
555557

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

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

src/content/3/fi/osa3c.md

Lines changed: 23 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -422,7 +422,7 @@ mongoose.set('strictQuery', false)
422422

423423
const url = process.env.MONGODB_URI // highlight-line
424424

425-
console.log('connecting to', url) // highlight-line
425+
console.log('connecting to', url)
426426
mongoose.connect(url)
427427
// highlight-start
428428
.then(result => {
@@ -449,17 +449,19 @@ noteSchema.set('toJSON', {
449449
module.exports = mongoose.model('Note', noteSchema) // highlight-line
450450
```
451451

452-
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ää:
453453

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+
const url = process.env.MONGODB_URI
456+
```
455457

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

458-
```js
459-
const Note = require('./models/note')
460+
```bash
461+
MONGODB_URI="osoite_tahan" npm run dev
460462
```
461463

462-
Näin muuttuja _Note_ saa arvokseen saman olion, jonka moduuli määrittelee.
464+
Opettelemme pian kehittyneemmän tavan määritellä ympäristömuuttujia.
463465

464466
Yhteyden muodostustavassa on pieni muutos aiempaan:
465467

@@ -477,27 +479,27 @@ Yhteyden muodostavalle metodille on nyt rekisteröity onnistuneen ja epäonnistu
477479

478480
![Konsoliin tulostuu virheilmoitus 'error connecting to Mongo, bad auth'](../../images/3/45e.png)
479481

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

482-
```js
483-
const url = process.env.MONGODB_URI
486+
Moduulin käyttöönotto tapahtuu lisäämällä tiedostoon <i>index.js</i> seuraava rivi:
484487

485-
console.log('connecting to', url)
488+
```js
489+
const Note = require('./models/note')
486490
```
487491

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

490-
```bash
491-
MONGODB_URI=osoite_tahan npm run dev
492-
```
494+
### Ympäristömuuttujien määritteleminen käyttäen dotenv-kirjastoa
493495

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
495497

496498
```bash
497499
npm install dotenv
498500
```
499501

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

502504
```bash
503505
MONGODB_URI=mongodb+srv://fullstack:[email protected]/noteApp?retryWrites=true&w=majority&appName=Cluster0
@@ -519,7 +521,7 @@ Ladataan ympäristömuuttujat käyttöön heti <i>index.js</i>-tiedoston alussa,
519521
```js
520522
require('dotenv').config() // highlight-line
521523
const express = require('express')
522-
const Note = require('./models/note')
524+
const Note = require('./models/note') // highlight-line
523525

524526
const app = express()
525527
// ..
@@ -532,9 +534,9 @@ app.listen(PORT, () => {
532534

533535
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.
534536

535-
### Tärkeä huomio ympäristömuuttujista
537+
#### Tärkeä huomio ympäristömuuttujien määrittelemisestä Fly.io:ssa ja Renderissä
536538

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

539541
[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ö
540542

@@ -548,7 +550,7 @@ ja asettaa ympäristömuuttujan arvo komennolla:
548550
fly secrets set MONGODB_URI='mongodb+srv://fullstack:[email protected]/noteApp?retryWrites=true&w=majority&appName=Cluster0'
549551
```
550552

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

553555
![](../../images/3/render-env.png)
554556

0 commit comments

Comments
 (0)