GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — add changes :koko: · knut0815/fullstack-javascript@7698959

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
  • chapter7
    • chapter7.md
Expand file treeCollapse file tree
Open diff view settings
Collapse file

‎chapter7/chapter7.md‎

Copy file name to clipboardExpand all lines: chapter7/chapter7.md
+31-45Lines changed: 31 additions & 45 deletions
  • Display the source diff
  • Display the rich diff
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,7 @@ CHAPTER 7
66
Intro to MongoDB
77
================
88

9-
> *What is Oracle? A bunch of people. And all of our products were just
10-
> ideas in the heads of those people - ideas that people typed into a
11-
> computer, tested, and that turned out to be the best idea for a
12-
> database or for a programming language.*
9+
> *What is Oracle? A bunch of people. And all of our products were just ideas in the heads of those people - ideas that people typed into a computer, tested, and that turned out to be the best idea for a database or for a programming language.*
1310
>
1411
> — [Larry Ellison](https://en.wikipedia.org/wiki/Larry_Ellison)
1512
@@ -39,17 +36,19 @@ For the MongoDB shell, or `mongo`, launch in a new terminal window (**important!
3936

4037
`$ ./bin/mongo`
4138

42-
You should see something like this, depending on your version of the
43-
MongoDB shell (`$ mongo --version` or after `$ mongo`):
39+
You should see something like this, depending on your version of the MongoDB shell (`$ mongo --version` or after `$ mongo`):
4440

45-
`MongoDB shell version: 3.0.6`
46-
`connecting to: test`
41+
```
42+
MongoDB shell version: 3.0.6
43+
connecting to: test
44+
```
4745

48-
To test the database, use the JavaScript-like interface and commands
49-
`save` and `find`:
46+
To test the database, use the JavaScript-like interface and commands `save` and `find`:
5047

51-
`> db.test.save( { a: 1 } )`
52-
`> db.test.find()`
48+
```
49+
> db.test.save( { a: 1 } )
50+
> db.test.find()
51+
```
5352

5453
Again, more detailed step-by-step instructions are available in the "Database: MongoDB" section of Chapter 2.
5554

@@ -154,9 +153,7 @@ const util = require('util')
154153
const mongodb = require ('mongodb')
155154
```
156155

157-
This is one of the ways to establish a connection to the MongoDB server in
158-
which the `db` variable will hold a reference to the database at a specified
159-
host and port:
156+
This is one of the ways to establish a connection to the MongoDB server in which the `db` variable will hold a reference to the database at a specified host and port:
160157

161158
```js
162159
const Db = mongodb.Db
@@ -177,9 +174,7 @@ db.open((error, connection) => {
177174
})
178175
```
179176

180-
To check that we have the connection, we need to handle `error`. Also,
181-
let's get the admin object with `db.admin()` and fetch the list of
182-
databases with `listDatabases()`:
177+
To check that we have the connection, we need to handle `error`. Also, let's get the admin object with `db.admin()` and fetch the list of databases with `listDatabases()`:
183178

184179
```js
185180
const db = new Db ('test', new Server(host, port, {}))
@@ -200,8 +195,7 @@ If we run it with `$ node app.js`, it should output "connected" in the terminal.
200195
console.log(util.inspect(db))
201196
```
202197

203-
Now you might want to set up the database in the cloud and test the
204-
connection from your Node.js script.
198+
Now you might want to set up the database in the cloud and test the connection from your Node.js script.
205199

206200
MongoDB on Heroku: MongoLab
207201
==================================================================================================================================
@@ -214,23 +208,25 @@ In order to connect to the database server, there is a database connection URL (
214208

215209
The database connection string `MONGOLAB_URI` has the following format:
216210

217-
mongodb://user:pass@server_NAME.mongolab.com:PORT/db_name
211+
```
212+
mongodb://user:pass@server_NAME.mongolab.com:PORT/db_name
213+
```
218214

219-
You could either copy the MongoLab URL string from the Heroku web site
220-
(and hard-code it) or get the string from the Node.js `process.env`
221-
object:
215+
You could either copy the MongoLab URL string from the Heroku web site (and hard-code it) or get the string from the Node.js `process.env` object:
222216

223-
`process.env.MONGOLAB_URI`
217+
```js
218+
process.env.MONGOLAB_URI
219+
```
224220

225221
or
226222

227-
`const connectionUri = url.parse(process.env.MONGOLAB_URI)`
223+
```js
224+
const connectionUri = url.parse(process.env.MONGOLAB_URI)
225+
```
228226

229227
The global object `process` gives access to environment variables via `process.env`. Heroku and Heroku add-ons like mLabs use these environment variables to pass database host names and ports, passwords, API keys, port numbers, and other system information that shouldn't be hard-coded into the main logic.
230228

231-
To make our code work both locally and on Heroku, we can use the logical
232-
OR operator `||` and assign a local host and port if environment
233-
variables are undefined:
229+
To make our code work both locally and on Heroku, we can use the logical OR operator `||` and assign a local host and port if environment variables are undefined:
234230

235231
```js
236232
const port = process.env.PORT || 1337
@@ -260,12 +256,9 @@ client.connect(dbConnUrl, {}, (error, db) => {
260256
})
261257
```
262258

263-
Following the modification of `app.js` by addition of `MONGOLAB_URI`, we
264-
can now initialize the Git repository, create a Heroku app, add the MongoLab
265-
add-on to it, and deploy the app with Git.
259+
Following the modification of `app.js` by addition of `MONGOLAB_URI`, we can now initialize the Git repository, create a Heroku app, add the MongoLab add-on to it, and deploy the app with Git.
266260

267-
Utilize the same steps as in the previous examples to create a new Git
268-
repository:
261+
Utilize the same steps as in the previous examples to create a new Git repository:
269262

270263
```
271264
$ git init
@@ -291,13 +284,9 @@ Add-ons work on a per app basis not on a per account basis. To install the free
291284
$ heroku addons:create mongolab:sandbox
292285
```
293286

294-
Or log on to Heroku (<https://elements.heroku.com/addons/mongolab>) with
295-
your Heroku credentials and choose MongoLab Free for that particular
296-
Heroku app, if you know the name of that app.
287+
Or log on to Heroku (<https://elements.heroku.com/addons/mongolab>) with your Heroku credentials and choose MongoLab Free for that particular Heroku app, if you know the name of that app.
297288

298-
The project folder needs to have `Procfile` and `package.json`. You can
299-
copy them from
300-
https://github.com/azat-co/fullstack-javascript/tree/master/code/07-db-connect-heroku.
289+
The project folder needs to have `Procfile` and `package.json`. You can copy them from `code/07-db-connect-heroku` or <http://bit.ly/2LeezQT>.
301290

302291
Now you can push your code to Heroku with:
303292

@@ -325,8 +314,7 @@ So far you have implemented a local `app.js` file (`code/07-db-connect/app.js` o
325314

326315
Let's enhance the latest `app.js` file by adding an HTTP server. After you get the `app.js` and the modified `app.js` files working, you modify the `app.js` to add a server so that the "connected" message will be displayed in the browser instead of the terminal window. To do so, we'll wrap the server object instantiation in a database connection callback. The final implementation is in the file `code/07-db-server/app.js` or at the book's GitHub repository: <http://bit.ly/2LcTd6K>.
327316

328-
Supplemental video which walks you through the implementation and
329-
demonstrates the project: <http://bit.ly/1Qnrmwr>.
317+
Supplemental video which walks you through the implementation and demonstrates the project: <http://bit.ly/1Qnrmwr>.
330318

331319
```js
332320
const util = require('util')
@@ -479,9 +467,7 @@ If you would like to use another domain name instead of http://your-app-name.her
479467

480468
Custom domains will hide the fact that your application is hosted on Heroku. For more information on custom domains can be found at <https://devcenter.heroku.com/articles/custom-domains>.
481469

482-
**Tip** For more productive and efficient development we should automate as
483-
much as possible; that is, use tests instead of CURL commands Use HTTP libraries such as `axios`, `superagent` or `request` to test your REST APIs. They are a timesaver for such tasks. There is
484-
a chapter on the Mocha library and Node.js testing in my other best-selling book *Practical Node.js, 2nd Edition* (Apress, 2018): <http://bit.ly/2LdCNL3> and <https://github.com/azat-co/practicalnode>.
470+
**Tip** For more productive and efficient development we should automate as much as possible; that is, use tests instead of CURL commands Use HTTP libraries such as `axios`, `superagent` or `request` to test your REST APIs. They are a timesaver for such tasks. There is a chapter on the Mocha library and Node.js testing in my other best-selling book *Practical Node.js, 2nd Edition* (Apress, 2018): <http://bit.ly/2LdCNL3> and <https://github.com/azat-co/practicalnode>.
485471

486472
Summary
487473
=======

0 commit comments

Comments
0 (0)
You are visitor 01402792 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.