GeezerVison is showing github.com — review chapter 6 · LukaszLevy/fullstack-javascript@0c0d336
File treeExpand file treeCollapse file tree Open diff view settings Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: chapter6/chapter6.md +6-6Lines changed: 6 additions & 6 deletions - Display the source diff
- Display the rich diff
| Original file line number | Diff line number | Diff line change |
|---|
@@ -73,7 +73,7 @@ environment or assign 1337 if the environment is not set, use: | 73 | 73 | This will create a server, and a callback function will contain the | 74 | 74 | response handler code: | 75 | 75 |
| 76 | | - const server = http.createServer(function (req, res) { | | 76 | + const server = http.createServer((req, res) => { | 77 | 77 |
| 78 | 78 | To set the right header and status code, use: | 79 | 79 |
| @@ -87,7 +87,7 @@ To output "Hello World" with the line end symbol, use: | 87 | 87 | To set a port and display the address of the server and the port number, | 88 | 88 | use: | 89 | 89 |
| 90 | | - server.listen(port, function() { | | 90 | + server.listen(port, () => { | 91 | 91 | console.log('Server is running at %s:%s ', | 92 | 92 | server.address().address, server.address().port) | 93 | 93 | }) | @@ -179,7 +179,7 @@ include: | 179 | 179 | - `util.inspect()`: Return a string representation of an object, | 180 | 180 | which is useful for debugging | 181 | 181 |
| 182 | | -###querystring | | 182 | +### querystring | 183 | 183 |
| 184 | 184 | This module provides utilities for dealing with query strings. Some of | 185 | 185 | the methods include: | @@ -215,22 +215,22 @@ The lists of non-core modules can be found at: | 215 | 215 | - [npmjs.org](https://npmjs.org): Node Package Manager registry | 216 | 216 |
| 217 | 217 | - Nipster (http://eirikb.github.io/nipster): NPM search | 218 | | - [Nipster](http://eirikb.github.com/nipster/): NPM search tool for | | 218 | + [Nipster](http://eirikb.github.com/nipster): NPM search tool for | 219 | 219 | Node.js | 220 | 220 |
| 221 | 221 | - [node-modules(http://node-modules.com): npm search engine | 222 | 222 | ](node-modules(http://node-modules.com):%20npm%20search%20engine%20%20%20%20%20) | 223 | 223 |
| 224 | 224 | If you would like to know how to code your own modules, take a look at | 225 | 225 | the article located here: | 226 | | -https://quickleft.com/blog/creating-and-publishing-a-node-js-module/ . | | 226 | +<https://quickleft.com/blog/creating-and-publishing-a-node-js-module>. | 227 | 227 |
| 228 | 228 | npm Node Package Manager | 229 | 229 | ------------------------ | 230 | 230 |
| 231 | 231 | <span id="manager" class="anchor"></span>Node Package Manager, or NPM, | 232 | 232 | manages dependencies and installs modules for you. Node.js installation | 233 | | -comes with NPM, whose web site is [npmjs.org](http://npmjs.org/). | | 233 | +comes with NPM, whose web site is [npmjs.org](http://npmjs.org). | 234 | 234 |
| 235 | 235 | `package.json` contains meta information about our Node.js application | 236 | 236 | such as a version number; author name; and, most important, what | |
Collapse file Copy file name to clipboard +6-5Lines changed: 6 additions & 5 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 1 | | -var http = require('http') // Loads http module | | 1 | +const http = require('http') // Loads http module | 2 | 2 |
| 3 | | -var port = process.env.PORT || 1337 | | 3 | +const port = process.env.PORT || 1337 | 4 | 4 |
| 5 | | -var server = http.createServer(function (req, res) { // Creates server | | 5 | +const server = http.createServer((req, res) => { // Creates server | 6 | 6 | res.writeHead(200, {'Content-Type': 'text/plain'}) // Sets the right header and status code | 7 | 7 | res.end('Hello World\n') // Outputs string with line end symbol | 8 | 8 | }) | 9 | | -server.listen(port, function() { | 10 | | - console.log('Server is running at %s:%s ', server.address().address, server.address().port) // Sets port and IP address of the server | | 9 | +server.listen(port, () => { | | 10 | + console.log('Server is running at %s:%s ', | | 11 | + server.address().address, server.address().port) // Sets port and IP address of the server | 11 | 12 | }) |
Collapse file Copy file name to clipboardExpand all lines: code/06-test/mb-server.js +7-7Lines changed: 7 additions & 7 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 1 | | -var http = require('http') | | 1 | +const http = require('http') | 2 | 2 | // Loads http module | 3 | | -var util= require('util') | | 3 | +const util= require('util') | 4 | 4 | // Usefull functions | 5 | | -var querystring = require('querystring') | | 5 | +const querystring = require('querystring') | 6 | 6 | // Loads querystring module, we'll need it to serialize and deserialize objects and query strings | 7 | 7 |
| 8 | | -var port = process.env.PORT || 1337 | | 8 | +const port = process.env.PORT || 1337 | 9 | 9 |
| 10 | | -var messages = [] | | 10 | +const messages = [] | 11 | 11 | // This array will hold our messages | 12 | 12 | messages.push({ | 13 | 13 | 'name': 'John', | @@ -19,7 +19,7 @@ exports.server = http.createServer(function (req, res) { | 19 | 19 | // Creates server | 20 | 20 | if (req.method == 'POST' && req.url == '/messages/create.json') { | 21 | 21 | // If method is POST and URL is messages/ add message to the array | 22 | | - var message = '' | | 22 | + let message = '' | 23 | 23 | req.on('data', function(data, msg){ | 24 | 24 | console.log(data.toString('utf-8')) | 25 | 25 | message = exports.addMessage(data.toString('utf-8')) | @@ -37,7 +37,7 @@ exports.server = http.createServer(function (req, res) { | 37 | 37 | }) | 38 | 38 | } else if (req.method == 'GET' && req.url == '/messages/list.json') { | 39 | 39 | // If method is GET and URL is /messages output list of messages | 40 | | - var body = exports.getMessages() | | 40 | + const body = exports.getMessages() | 41 | 41 | // Body will hold our output | 42 | 42 | res.writeHead(200, { | 43 | 43 | 'Content-Length': body.length, | |
Collapse file Copy file name to clipboardExpand all lines: code/06-test/test.js +5-5Lines changed: 5 additions & 5 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 1 | | -var http = require('http') | 2 | | -var assert = require('assert') | 3 | | -var querystring = require('querystring') | 4 | | -var util = require('util') | | 1 | +const http = require('http') | | 2 | +const assert = require('assert') | | 3 | +const querystring = require('querystring') | | 4 | +const util = require('util') | 5 | 5 |
| 6 | | -var messageBoard = require('./mb-server') | | 6 | +const messageBoard = require('./mb-server') | 7 | 7 |
| 8 | 8 | assert.deepEqual('[{"name":"John","message":"hi"}]', | 9 | 9 | messageBoard.getMessages()) | |
|