You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+40-21Lines changed: 40 additions & 21 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -36,12 +36,23 @@ This is a pretty good intro video:
36
36
Node v4.0.0 is now available and supports lots of ES6 features.
37
37
(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).
38
38
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)!
40
40
41
-
### So what is a server??
41
+
### So what is a server? And why do I need one?
42
42
43
43
***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.
44
44
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
+
45
56
## Installing Node
46
57
47
58
Download Node from the [NodeJS website](https://docs.npmjs.com/getting-started/installing-node)
@@ -77,9 +88,10 @@ Check the version
77
88
78
89
```js
79
90
$ npm --version
91
+
// the version should be 3.3.4
80
92
```
81
93
82
-
Then type:
94
+
If it is not the latest version then type:
83
95
84
96
```js
85
97
sudo npm install npm -g
@@ -128,8 +140,11 @@ This takes you through the process of creating a file called a `package.json` wh
@@ -187,12 +202,11 @@ var mandrill = require('mandrill'); //Mandrill is a module for setting up an ema
187
202
188
203
## Structuring a module
189
204
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.
191
206
192
207
```js
193
-
var Library = {
194
-
195
-
books: {
208
+
var Library = (function() {
209
+
var books = {
196
210
"Emma": {
197
211
author:'Jane Austen',
198
212
published:'December 25, 1815'
@@ -202,28 +216,30 @@ var Library = {
202
216
author:'JK Rowling',
203
217
published:'July 8, 2000'
204
218
}
205
-
}
219
+
};
206
220
207
-
getBookAuthor:function(name) {
221
+
functiongetBookAuthor =function(name) {
208
222
return books[name].author
209
-
},
223
+
};
210
224
211
-
getDatePublished:function() {
225
+
functiongetDatePublished =function() {
212
226
return books[name].published
213
-
},
227
+
};
214
228
215
229
return {
216
230
getBook: getBook,
217
231
getDatePublished: getDatePublished
218
232
}
219
-
}
233
+
234
+
}());
220
235
221
236
```
222
237
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.
223
238
224
-
Add this line to the end of myModule.js:
239
+
Add this line to the end of library.js:
225
240
226
241
```js
242
+
227
243
module.exports= Library;
228
244
229
245
```
@@ -272,19 +288,22 @@ var port = process.env.PORT || 8000;
console.log('node http server listening on http://localhost:'+ port);
285
304
```
286
305
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:
288
307
289
308
****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.
290
309
@@ -353,7 +372,7 @@ var request = new XMLHttpRequest();
353
372
On the server you would look at the url of the request:
354
373
355
374
```js
356
-
http.createServer(request, response) {
375
+
function(request, response) {
357
376
var url =request.url
358
377
if (url.indexOf('/cat') >-1) {
359
378
// 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:
404
423
405
424
## Event Loop and EventEmitters
406
425
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.
0 commit comments