@@ -6,10 +6,7 @@ CHAPTER 7 |
6 | 6 | Intro to MongoDB |
7 | 7 | ================ |
8 | 8 |
|
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.* |
13 | 10 | > |
14 | 11 | > — [Larry Ellison](https://en.wikipedia.org/wiki/Larry_Ellison) |
15 | 12 |
|
@@ -39,17 +36,19 @@ For the MongoDB shell, or `mongo`, launch in a new terminal window (**important! |
39 | 36 |
|
40 | 37 | `$ ./bin/mongo` |
41 | 38 |
|
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`): |
44 | 40 |
|
45 | | -`MongoDB shell version: 3.0.6` |
46 | | -`connecting to: test` |
| 41 | +``` |
| 42 | +MongoDB shell version: 3.0.6 |
| 43 | +connecting to: test |
| 44 | +``` |
47 | 45 |
|
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`: |
50 | 47 |
|
51 | | -`> db.test.save( { a: 1 } )` |
52 | | -`> db.test.find()` |
| 48 | +``` |
| 49 | +> db.test.save( { a: 1 } ) |
| 50 | +> db.test.find() |
| 51 | +``` |
53 | 52 |
|
54 | 53 | Again, more detailed step-by-step instructions are available in the "Database: MongoDB" section of Chapter 2. |
55 | 54 |
|
@@ -154,9 +153,7 @@ const util = require('util') |
154 | 153 | const mongodb = require ('mongodb') |
155 | 154 | ``` |
156 | 155 |
|
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: |
160 | 157 |
|
161 | 158 | ```js |
162 | 159 | const Db = mongodb.Db |
@@ -177,9 +174,7 @@ db.open((error, connection) => { |
177 | 174 | }) |
178 | 175 | ``` |
179 | 176 |
|
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()`: |
183 | 178 |
|
184 | 179 | ```js |
185 | 180 | 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. |
200 | 195 | console.log(util.inspect(db)) |
201 | 196 | ``` |
202 | 197 |
|
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. |
205 | 199 |
|
206 | 200 | MongoDB on Heroku: MongoLab |
207 | 201 | ================================================================================================================================== |
@@ -214,23 +208,25 @@ In order to connect to the database server, there is a database connection URL ( |
214 | 208 |
|
215 | 209 | The database connection string `MONGOLAB_URI` has the following format: |
216 | 210 |
|
217 | | - mongodb://user:pass@server_NAME.mongolab.com:PORT/db_name |
| 211 | +``` |
| 212 | +mongodb://user:pass@server_NAME.mongolab.com:PORT/db_name |
| 213 | +``` |
218 | 214 |
|
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: |
222 | 216 |
|
223 | | -`process.env.MONGOLAB_URI` |
| 217 | +```js |
| 218 | +process.env.MONGOLAB_URI |
| 219 | +``` |
224 | 220 |
|
225 | 221 | or |
226 | 222 |
|
227 | | -`const connectionUri = url.parse(process.env.MONGOLAB_URI)` |
| 223 | +```js |
| 224 | +const connectionUri = url.parse(process.env.MONGOLAB_URI) |
| 225 | +``` |
228 | 226 |
|
229 | 227 | 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. |
230 | 228 |
|
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: |
234 | 230 |
|
235 | 231 | ```js |
236 | 232 | const port = process.env.PORT || 1337 |
@@ -260,12 +256,9 @@ client.connect(dbConnUrl, {}, (error, db) => { |
260 | 256 | }) |
261 | 257 | ``` |
262 | 258 |
|
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. |
266 | 260 |
|
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: |
269 | 262 |
|
270 | 263 | ``` |
271 | 264 | $ git init |
@@ -291,13 +284,9 @@ Add-ons work on a per app basis not on a per account basis. To install the free |
291 | 284 | $ heroku addons:create mongolab:sandbox |
292 | 285 | ``` |
293 | 286 |
|
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. |
297 | 288 |
|
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>. |
301 | 290 |
|
302 | 291 | Now you can push your code to Heroku with: |
303 | 292 |
|
@@ -325,8 +314,7 @@ So far you have implemented a local `app.js` file (`code/07-db-connect/app.js` o |
325 | 314 |
|
326 | 315 | 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>. |
327 | 316 |
|
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>. |
330 | 318 |
|
331 | 319 | ```js |
332 | 320 | const util = require('util') |
@@ -479,9 +467,7 @@ If you would like to use another domain name instead of http://your-app-name.her |
479 | 467 |
|
480 | 468 | 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>. |
481 | 469 |
|
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>. |
485 | 471 |
|
486 | 472 | Summary |
487 | 473 | ======= |
|