Skip to content

Commit 2149b19

Browse files
committed
adds more info to readme and modifies server handler
1 parent 304cb84 commit 2149b19

File tree

1 file changed

+40
-21
lines changed

1 file changed

+40
-21
lines changed

README.md

Lines changed: 40 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -36,12 +36,23 @@ This is a pretty good intro video:
3636
Node v4.0.0 is now available and supports lots of ES6 features.
3737
(ES6 - aka ECMAScript 6 or ECMAScript 2015 - is a newer version of JavaScript with a bunch of new features. It's not 100% supported everywhere yet, but it will be eventually).
3838

39-
If you want to learn ES6 Look up @benjaminlees [tutorial](https://github.com/benjaminlees/Es6)!
39+
If you want to learn ES6 Look up [@benjaminlees](https://github.com/nelsonic)'s [tutorial](https://github.com/benjaminlees/Es6)!
4040

41-
### So what is a server??
41+
### So what is a server? And why do I need one?
4242

4343
***Servers*** are computer programs that receive requests from other programs, ***the clients*** and send back a response e.g share data, information or hardware and software resources.
4444

45+
In a typical web app a server could perform some of these functions:
46+
47+
* Handle manipulation of data in the database
48+
* File manipulation
49+
* Authentication
50+
* Lots of secret logic
51+
52+
Client side code sends requests to a server which sends back data to the front end which can then be displayed.
53+
54+
Front end Javascript is executed in the site visitor's browser whereas server-side code runs on a site's web server
55+
4556
## Installing Node
4657

4758
Download Node from the [NodeJS website](https://docs.npmjs.com/getting-started/installing-node)
@@ -77,9 +88,10 @@ Check the version
7788

7889
```js
7990
$ npm --version
91+
// the version should be 3.3.4
8092
```
8193

82-
Then type:
94+
If it is not the latest version then type:
8395

8496
```js
8597
sudo npm install npm -g
@@ -128,8 +140,11 @@ This takes you through the process of creating a file called a `package.json` wh
128140
"url": "https://github.com/nikhilaravi/autocomplete/issues"
129141
},
130142
"homepage": "https://github.com/nikhilaravi/autocomplete",
143+
"engines": {
144+
"node": ">= 0.10"
145+
},
131146
"dependancies": {
132-
"node":
147+
"pre-commit": "^1.0.7"
133148
}
134149
}
135150

@@ -187,12 +202,11 @@ var mandrill = require('mandrill'); //Mandrill is a module for setting up an ema
187202

188203
## Structuring a module
189204

190-
Good practice for modules is to create one object in your file that contains all the methods and return only the methods. This way all the variables aren't exposed and can't be modified by other files.
205+
Good practice for modules is to create an object in your file with an immediately invoked function (IIFE) that returns only the methods. This way all the variables aren't exposed and can't be modified by other files.
191206

192207
```js
193-
var Library = {
194-
195-
books: {
208+
var Library = (function() {
209+
var books = {
196210
"Emma": {
197211
author: 'Jane Austen',
198212
published: 'December 25, 1815'
@@ -202,28 +216,30 @@ var Library = {
202216
author: 'JK Rowling',
203217
published: 'July 8, 2000'
204218
}
205-
}
219+
};
206220

207-
getBookAuthor: function(name) {
221+
function getBookAuthor = function(name) {
208222
return books[name].author
209-
},
223+
};
210224

211-
getDatePublished: function() {
225+
function getDatePublished = function() {
212226
return books[name].published
213-
},
227+
};
214228

215229
return {
216230
getBook: getBook,
217231
getDatePublished: getDatePublished
218232
}
219-
}
233+
234+
}());
220235

221236
```
222237
To enable the functions to be used by other files, you need to export the object. Save this file as e.g. library.js. Other files can then import this file and use the methods returned.
223238

224-
Add this line to the end of myModule.js:
239+
Add this line to the end of library.js:
225240

226241
```js
242+
227243
module.exports = Library;
228244

229245
```
@@ -272,19 +288,22 @@ var port = process.env.PORT || 8000;
272288
```
273289

274290
```js
275-
http.createServer(function handler(request, response) {
291+
292+
function handler(request, response) {
276293
//display 'HELLO WORLD' when the user is on the home page
277294
var url = request.url; //e.g. '/'
278295
if (url.length === 1) {
279296
response.writeHead(200, {"Content-Type": "text/html"});
280297
response.end("HELLO WORLD!");
281298
}
282-
}).listen(port);
299+
}
300+
301+
http.createServer(handler).listen(port);
283302

284303
console.log('node http server listening on http://localhost:' + port);
285304
```
286305

287-
Inside the call to `http.createServer()' we pass in a function that gets called every time someone connects to the app. The function takes two parameters:
306+
Inside the call to `http.createServer()' we pass in a handler function that gets called every time someone connects to the app. The function takes two parameters:
288307

289308
* ***request*** - this object contains the information about what the visitor asked for including name of the page that was requested, the settings, and any fields filled in on a form.
290309

@@ -353,7 +372,7 @@ var request = new XMLHttpRequest();
353372
On the server you would look at the url of the request:
354373

355374
```js
356-
http.createServer(request, response) {
375+
function(request, response) {
357376
var url = request.url
358377
if (url.indexOf('/cat') > -1) {
359378
// check if the url contains /cat and if so send back a link to a cat image e.g. from a database or an API
@@ -404,7 +423,7 @@ In the scripts part of your package.json add the following line:
404423
405424
## Event Loop and EventEmitters
406425
407-
If you make a post request and send some data with the request you need a way of reading the data on the server side. For this you need to listen for the 'data' event on the request.
426+
If you make a post request and send some data with the request you need a way of reading the data on the server side. For this you need to listen for the 'data' event on the request.
408427
409428
```js
410429
var requestData = '';
@@ -417,7 +436,7 @@ request.on('data', function(chunk) {
417436
418437
If you are using APIs, you don't want to push the API Keys up to Github. To keep the keys secret we want to save them as environment variables.
419438
420-
Follow @nelsonic's tutorial to learn how this works!
439+
Follow [@nelsonic](https://github.com/nelsonic)'s tutorial to learn how this works!
421440
[https://github.com/dwyl/learn-environment-variables](https://github.com/dwyl/learn-environment-variables)
422441
423442
## Install the Node.js version manager module

0 commit comments

Comments
 (0)