@@ -13,39 +13,25 @@ Intro to Node.js |
13 | 13 | In this chapter, we'll cover the following: |
14 | 14 |
|
15 | 15 | - Building "Hello World" in Node.js |
16 | | - |
17 | | -- Node.js Core Modules |
18 | | - |
19 | | -- npm Node Package Manager |
20 | | - |
21 | | -- Message Board with Node.js: Memory Store Version |
22 | | - |
23 | | -- Unit Testing Node.js |
| 16 | +- Node.js core modules |
| 17 | +- npm Node package manager |
| 18 | +- Message Board with Node.js: memory store version |
| 19 | +- Unit testing Node.js |
24 | 20 |
|
25 | 21 | Node.js is a non-blocking platform for building web applications. It |
26 | | -uses JavaScript, so it's a centerpiece in our fullstack JavaScript |
27 | | -development. We'll start with Hello World and cover core modules and |
28 | | -npm. Then, we deploy our Hello World app to cloud. |
| 22 | +uses JavaScript, so it's a centerpiece in our full stack JavaScript |
| 23 | +development. We'll start by building our "Hello World" app and then we'll cover core modules and |
| 24 | +npm. Then, we deploy our "Hello World" app to the cloud. |
29 | 25 |
|
30 | 26 | Building "Hello World" in Node.js |
31 | 27 | ================================= |
32 | 28 |
|
33 | | -<span id="OLE_LINK27" class="anchor"><span id="OLE_LINK26" |
34 | | -class="anchor"><span id="OLE_LINK36" class="anchor"><span |
35 | | -id="OLE_LINK38" class="anchor"><span id="OLE_LINK39" |
36 | | -class="anchor"><span id="buildinghelloworld" |
37 | | -class="anchor"></span></span></span></span></span></span>Supplemental |
38 | | -video which walks you through the implementation and demonstrates the |
39 | | -project: http://bit.ly/1QnqFmF. |
40 | | - |
41 | 29 | To check if you have Node.js installed on your computer, type and |
42 | 30 | execute this command in your terminal: |
43 | 31 |
|
44 | 32 | $ node -v |
45 | 33 |
|
46 | | -As of this writing, the latest version is 8.11.1. If you don't have |
47 | | -Node.js installed, or if your version is behind, you can download the |
48 | | -latest version at [nodejs.org/#download](http://nodejs.org/#download). |
| 34 | +Get the version 8 or higher. If you don't have Node.js installed, or if your version is older (behind or lower), you can download the latest version at <http://nodejs.org/#download>. |
49 | 35 | You can use one of these tools for version management (i.e., switching |
50 | 36 | between Node.js versions): |
51 | 37 |
|
@@ -55,13 +41,13 @@ between Node.js versions): |
55 | 41 |
|
56 | 42 | - [nvm](https://github.com/creationix/nvm) (<https://github.com/creationix/nvm>) |
57 | 43 |
|
58 | | -As usual, you could copy example code at |
59 | | -https://github.com/azat-co/fullstack-javascript/tree/master/code/06-hello, or |
| 44 | +As usual, you could copy the example code at |
| 45 | +https://github.com/azat-co/fullstack-javascript/tree/master/code/06-hello, TK or |
60 | 46 | write your own program from scratch. If you wish to do the latter, |
61 | 47 | create a folder `hello` for your "Hello World" Node.js application. Then |
62 | | -create file a `server.js` and line by line type the code below. |
| 48 | +create a file `server.js` and line by line type the code below. |
63 | 49 |
|
64 | | -This will load the core `http` module for the server (more on the |
| 50 | +This line will load the core `http` module for the server (more on the |
65 | 51 | modules later): |
66 | 52 |
|
67 | 53 | const http = require('http') |
@@ -98,21 +84,20 @@ the following command: |
98 | 84 |
|
99 | 85 | $ node server.js |
100 | 86 |
|
101 | | -Open [localhost:1337](http://localhost:1337/) or |
102 | | -[127.0.0.1:1337](http://127.0.0.1:1337/) or any other address you see in |
103 | | -the terminal as a result of `console.log()` function, and you should see |
| 87 | +Open <http://localhost:1337> or <http://127.0.0.1:1337> or any other address you see in |
| 88 | +the terminal as a result of the `console.log()` function, and you should see |
104 | 89 | "Hello World" in a browser. To shut down the server, press Control + C. |
105 | 90 |
|
106 | | -Note: The name of the main file could be different from server.js (e.g., |
107 | | -index.js or app.js). In case you need to launch the `app.js` file, just |
| 91 | +**Note** The name of the main file could be different from `server.js` (e.g., |
| 92 | +`index.js` or `app.js`). In case you need to launch the `app.js` file, just |
108 | 93 | use `$ node app.js`. |
109 | 94 |
|
110 | 95 | Node.js Core Modules |
111 | 96 | ==================== |
112 | 97 |
|
113 | 98 | Unlike other programming technologies, Node.js doesn't come with a heavy |
114 | | -standard library. The core modules of node.js are a bare minimum and the |
115 | | -rest can be cherry-picked via the Node Package Manager (npm) registry. |
| 99 | +standard library. The core modules of Node.js are a bare minimum and the |
| 100 | +rest can be cherry-picked via the npm Node package manager registry. |
116 | 101 | The main core modules, classes, methods, and events include: |
117 | 102 |
|
118 | 103 | - [http](http://nodejs.org/api/http.html) |
@@ -124,7 +109,7 @@ The main core modules, classes, methods, and events include: |
124 | 109 |
|
125 | 110 | - [querystring](http://nodejs.org/api/querystring.html) |
126 | 111 | (<https://nodejs.org/api/querystring.html>): Module for parsing query |
127 | | - string from the URI |
| 112 | + strings from the URI |
128 | 113 |
|
129 | 114 | - [url](http://nodejs.org/api/url.html) |
130 | 115 | (<https://nodejs.org/api/url.html>): Module for parsing URI |
@@ -134,77 +119,61 @@ The main core modules, classes, methods, and events include: |
134 | 119 | (<https://nodejs.org/api/fs.html>): Module for working with the file |
135 | 120 | system |
136 | 121 |
|
137 | | -These are the most important core modules. Let's cover each of them. |
| 122 | +These are the most important core modules. Let's take a look at each of them. |
138 | 123 |
|
139 | 124 | ### http |
140 | 125 |
|
141 | | - |
142 | | -This is the main module responsible for Node.js HTTP server. Here are |
| 126 | +This is the main module responsible for the Node.js HTTP server. Here are |
143 | 127 | the main methods: |
144 | 128 |
|
145 | | -- `http.createServer()`: returns a new web server object |
146 | | - |
147 | | -- `http.listen()`: begins accepting connections on the specified |
| 129 | +- `http.createServer()`: Returns a new web server object |
| 130 | +- `http.listen()`: Begins accepting connections on the specified |
148 | 131 | port and hostname |
149 | | - |
150 | | -- `http.createClient()`: node app can be a client and make requests |
151 | | - to other servers |
152 | | - |
153 | | -- `http.ServerRequest()`: incoming requests are passed to request |
154 | | - handlers |
155 | | - |
156 | | - - `data`: emitted when a piece of the message body is received |
157 | | - |
158 | | - - `end`: emitted exactly once for each request |
159 | | - |
160 | | - - `request.method()`: the request method as a string |
161 | | - |
162 | | - - `request.url()`: request URL string |
163 | | - |
164 | | -- `http.ServerResponse()`: this object is created internally by an |
165 | | - HTTP server—not by the user, and used as an output of request |
| 132 | +- `http.createClient()`: Node app can be a client and make requests |
| 133 | + to other servers TK |
| 134 | +- `http.ServerRequest()`: Passes incoming requests to request |
166 | 135 | handlers |
| 136 | + - `data`: Emitted when a piece of the message body is received |
| 137 | + - `end`: Emitted exactly once for each request |
| 138 | + - `request.method()`: The request method as a string |
| 139 | + - `request.url()`: Request URL string |
167 | 140 |
|
168 | | - - `response.writeHead()`: sends a response header to the request |
169 | | - |
170 | | - - `response.write()`: sends a response body |
| 141 | +- `http.ServerResponse()`: Provides response/output of request handlers initiated by an |
| 142 | + HTTP server—not by the user |
171 | 143 |
|
172 | | - - `response.end()`: sends and ends a response body |
| 144 | + - `response.writeHead()`: Sends a response header to the request |
| 145 | + - `response.write()`: Sends a response body |
| 146 | + - `response.end()`: Sends and ends a response body |
173 | 147 |
|
174 | 148 | ### util |
175 | 149 |
|
| 150 | +This module provides a utility for debugging: |
176 | 151 |
|
177 | | -This module provides utilities for debugging. Some of the methods |
178 | | -include: |
179 | | - |
180 | | -- `util.inspect()`: Return a string representation of an object, |
| 152 | +- `util.inspect()`: Returns a string representation of an object, |
181 | 153 | which is useful for debugging |
182 | 154 |
|
183 | 155 | ### querystring |
184 | 156 |
|
185 | 157 | This module provides utilities for dealing with query strings. Some of |
186 | 158 | the methods include: |
187 | 159 |
|
188 | | -- `querystring.stringify()`: Serialize an object to a query string |
189 | | - |
190 | | -- `querystring.parse()`: Deserialize a query string to an object |
| 160 | +- `querystring.stringify()`: Serializes an object to a query string |
| 161 | +- `querystring.parse()`: Deserializes a query string to an object |
191 | 162 |
|
192 | 163 | ### url |
193 | 164 |
|
194 | | -This module has utilities for URL resolution and parsing. Some of the |
195 | | -methods include: |
| 165 | +This module has a utility for URL resolution and parsing: |
196 | 166 |
|
197 | | -- `parse()`: Take a URL string, and return an object |
| 167 | +- `url.parse()`: Takes a URL string, and returns an object which has URL information broken down into parts |
198 | 168 |
|
199 | 169 | ### fs |
200 | 170 |
|
201 | | -fs handles file system operations such as reading and writing to/from |
| 171 | +`fs` handles file system operations such as reading and writing to/from |
202 | 172 | files. There are synchronous and asynchronous methods in the library. |
203 | 173 | Some of the methods include: |
204 | 174 |
|
205 | | -- `fs.readFile()`: reads file asynchronously |
206 | | - |
207 | | -- `fs.writeFile()`: writes data to file asynchronously |
| 175 | +- `fs.readFile()`: Reads file asynchronously |
| 176 | +- `fs.writeFile()`: Writes data to file asynchronously |
208 | 177 |
|
209 | 178 | There is no need to install or download core modules. To include them in |
210 | 179 | your application, all you need is to follow the syntax: |
@@ -214,13 +183,9 @@ your application, all you need is to follow the syntax: |
214 | 183 | The lists of non-core modules can be found at: |
215 | 184 |
|
216 | 185 | - [npmjs.org](https://npmjs.org): Node Package Manager registry |
217 | | - |
218 | | -- Nipster (http://eirikb.github.io/nipster): npm search |
219 | | - [Nipster](http://eirikb.github.com/nipster): npm search tool for |
| 186 | +- Nipster (<http://eirikb.github.io/nipster>): npm search tool for |
220 | 187 | Node.js |
221 | | - |
222 | | -- [node-modules(http://node-modules.com): npm search engine |
223 | | - ](node-modules(http://node-modules.com):%20npm%20search%20engine%20%20%20%20%20) |
| 188 | +- node-modules (<http://node-modules.com>): npm search engine |
224 | 189 |
|
225 | 190 | If you would like to know how to code your own modules, take a look at |
226 | 191 | the article located here: |
@@ -276,18 +241,32 @@ A typical `package.json` file might look like this: |
276 | 241 |
|
277 | 242 | While most of the properties in the `package.json` example above like |
278 | 243 | `description` and `name` are self-explanatory, others deserve more |
279 | | -explaining. Dependencies is an object, and each item has the name on the |
280 | | -left side and the version number on the right side (e.g., "express": |
281 | | -">= 2.5.6"). The version can be exact: for example, "express": |
282 | | -"2.5.6," or greater than, or wild-card, for example, "express": "\*" (a |
283 | | -great way to blow up your app in production with new untested |
284 | | -dependencies: therefore not recommended). |
| 244 | +explaining. The `dependencies` property is an object, and each item has the name on the |
| 245 | +left side and the version number on the right side. For example, this statement tells npm to use Express version 2.5.6 or lower (earlier): |
| 246 | + |
| 247 | +```js |
| 248 | +"express": "<= 2.5.6" |
| 249 | +``` |
| 250 | + |
| 251 | +The version can be exact (recommended). For example, this statement locks the version of Express at 2.5.6.: |
| 252 | + |
| 253 | +```js |
| 254 | +"express": "2.5.6," |
| 255 | +``` |
| 256 | + |
| 257 | +The versions can be specified to be greater-than (`>`), less-than (`<`), or any/wildcard (`*`). For example, this statement tell npm to use any version which usually means npm will get the latest stable version: |
| 258 | + |
| 259 | +```js |
| 260 | +"express": "*" |
| 261 | +``` |
| 262 | + |
| 263 | +A wild card is a great way to blow up your app in production with new untested |
| 264 | +dependencies: therefore not recommended. |
285 | 265 |
|
286 | 266 | The `bin` property is for command-line utilities. It tells the system |
287 | 267 | what file to launch. And the `scripts` object has scripts that you can |
288 | | -launch with `$ ``npm` `run SCRIPT_NAME`. The `start` script and test are |
289 | | -exceptions. You can run them with `$ ``npm`` start` and |
290 | | -`$ ``npm`` test`. |
| 268 | +launch with `$ npm run SCRIPT_NAME`. The `start` and `test` scripts are |
| 269 | +exceptions. You can run them with `$ npm start` and `$ npm test`. |
291 | 270 |
|
292 | 271 | To update a package to its current latest version or the latest version |
293 | 272 | that is allowable by the version specification defined in |
@@ -300,14 +279,14 @@ Or for single module installation: |
300 | 279 | $ npm install name-of-the-package |
301 | 280 |
|
302 | 281 | The only module used in this book's examples—and which does not belong |
303 | | -to the core Node.js package—is `mongodb`. We'll install it later in the |
304 | | -book. |
| 282 | +to the core Node.js package—is `mongodb`. We'll install it in the next chapter. |
| 283 | + |
| 284 | +However, Heroku will need `package.json` to run npm on the server. The easiest way to create `package.json` is to execute: |
305 | 285 |
|
306 | | -Heroku will need `package.json` to run npm on the server. |
| 286 | +``` |
| 287 | +$ npm init -y |
| 288 | +``` |
307 | 289 |
|
308 | | -For more information on npm, take a look at the article "[Tour of |
309 | | -npm](http://tobyho.com/2012/02/09/tour-of-npm/)" |
310 | | -(http://tobyho.com/2012/02/09/tour-of-npm) |
311 | 290 |
|
312 | 291 | Deploying "Hello World" to PaaS |
313 | 292 | =============================== |
@@ -318,26 +297,31 @@ your terminal: |
318 | 297 |
|
319 | 298 | $ git init |
320 | 299 |
|
321 | | -Git will create a hidden `.``git` folder. Now we can add files and make |
| 300 | +Git will create a hidden `.git` folder. Now we can add files and make |
322 | 301 | the first commit: |
323 | 302 |
|
324 | 303 | $ git add . |
325 | 304 | $ git commit -am "first commit" |
326 | 305 |
|
327 | | -Tip: To view hidden files on the macOS Finder app, execute this |
| 306 | +**Tip** To view hidden files on the macOS Finder app, execute this |
328 | 307 | command in a terminal window: |
329 | | -`defaults write ``com.apple.finder`` ``AppleShowAllFiles`` -``bool`` true`. |
| 308 | + |
| 309 | +``` |
| 310 | +defaults write com.apple.finder AppleShowAllFiles -bool true |
| 311 | +``` |
| 312 | + |
330 | 313 | To change the flag back to hidden: |
331 | | -`defaults write ``com.apple.finder`` ``AppleShowAllFiles`` -``bool`` false`. |
| 314 | + |
| 315 | +``` |
| 316 | +defaults write com.apple.finder AppleShowAllFiles -bool false |
| 317 | +``` |
332 | 318 |
|
333 | 319 | Deploying to Microsoft Azure |
334 | 320 | ========================== |
335 | 321 |
|
336 | | -In order to deploy our "Hello World" application to Microsoft Azure, we |
337 | | -must add Git **remote**. You could copy the URL from Microsoft Azure |
338 | | -Portal, under Web Site, and use it with this command: |
| 322 | +In order to deploy our "Hello World" application to Microsoft Azure, we must add a Git remote destination that belongs to Azure. You could copy the URI/URL from the Microsoft Azure Portal, and use it with this command: |
339 | 323 |
|
340 | | - $ git remote add azure yourURL |
| 324 | + $ git remote add azure YOUR_AZURE_URI |
341 | 325 |
|
342 | 326 | Now we should be able to make a push with this command: |
343 | 327 |
|
@@ -352,15 +336,14 @@ To push changes, just execute: |
352 | 336 | $ git commit -m "changing to hello azure" |
353 | 337 | $ git push azure master |
354 | 338 |
|
355 | | -A more meticulous guide can be found in the tutorial |
356 | | -https://azure.microsoft.com/en-us/documentation/articles/web-sites-nodejs-develop-deploy-mac. |
| 339 | +A more meticulous guide can be found in the tutorial <https://docs.microsoft.com/en-us/azure/app-service/app-service-web-get-started-nodejs>. TK |
357 | 340 |
|
358 | 341 | Deploying to Heroku |
359 | 342 | =================== |
360 | 343 |
|
361 | 344 | For Heroku deployment, we need to create two extra files: `Procfile` and |
362 | 345 | `package.json`. You could get the source code from |
363 | | -https://github.com/azat-co/fullstack-javascript/tree/master/code/06-hello or |
| 346 | +<https://github.com/azat-co/fullstack-javascript/tree/master/code/06-hello> TK or |
364 | 347 | write your own one. |
365 | 348 |
|
366 | 349 | The structure of the "Hello World" application looks like this: |
@@ -370,9 +353,9 @@ The structure of the "Hello World" application looks like this: |
370 | 353 | -Procfile |
371 | 354 | -server.js |
372 | 355 |
|
373 | | -Procfile is a mechanism for declaring what commands are run by your |
| 356 | +`Procfile` is a mechanism for declaring what commands are run by your |
374 | 357 | application’s dynos on the Heroku platform. Basically, it tells Heroku |
375 | | -what processes to run. Procfile has only one line in this case: |
| 358 | +what processes to run. `Procfile` has only one line in this case: |
376 | 359 |
|
377 | 360 | web: node server.js |
378 | 361 |
|
@@ -391,7 +374,7 @@ For this example, we keep `package.json` simple: |
391 | 374 | After we have all of the files in the project folder, we can use Git to |
392 | 375 | deploy the application. The commands are pretty much the same as with |
393 | 376 | Microsoft Azure except that we need to add Git remote, and create Cedar |
394 | | -stack with: |
| 377 | +Stack with: |
395 | 378 |
|
396 | 379 | $ heroku create |
397 | 380 |
|
@@ -408,12 +391,8 @@ If everything went okay, you should see success logs in the terminal and |
408 | 391 | Message Board with Node.js: Memory Store Version |
409 | 392 | ------------------------------------------------ |
410 | 393 |
|
411 | | -Supplemental video which walks you through the implementation and |
412 | | -demonstrates the project: http://bit.ly/1QnqO9P. |
413 | | - |
414 | 394 | The first version of the Message Board back-end application will store |
415 | | -messages only in runtime memory storage for the sake of KISS |
416 | | -(http://en.wikipedia.org/wiki/KISS_principle). That means that each |
| 395 | +messages only in runtime memory storage for the sake of the KISS principle—keep it simple stupid (<http://azat.co/blog/kiss>). That means that each |
417 | 396 | time we start/reset the server, the data will be lost. |
418 | 397 |
|
419 | 398 | We'll start with a simple test case first to illustrate the Test-Driven |
|