Skip to content

Commit ecdd443

Browse files
committed
Updating labs
1 parent eedb4ba commit ecdd443

2 files changed

Lines changed: 249 additions & 225 deletions

File tree

labs/HTTP.md

Lines changed: 136 additions & 136 deletions
Original file line numberDiff line numberDiff line change
@@ -12,119 +12,119 @@
1212
- How to bind an HTTP server to a port.
1313
- How to accept HTTP requests.
1414
- How to respond to incoming HTTP requests.
15-
- HTTP Status: 200 OK
16-
- With content
15+
- HTTP Status: 200 OK
16+
- With content
1717

1818
## Lab
1919

2020
In this lab we will create an HTTP server that responds to all requests with a simple HTML template.
2121

2222
1. Create a new file named ```server.js``` in a directory of your choice.
2323
2. Include the HTTP core module using the ```require(moduleName)``` function and assign the return value to a variable named ```http```.
24-
25-
```JavaScript
26-
var http = require('http');
27-
```
24+
25+
```JavaScript
26+
var http = require('http');
27+
```
2828

2929
3. To create a HTTP server execute the ```http.createServer``` ([api doc](http://nodejs.org/api/http.html#http_http_createserver_requestlistener)) function with an anonymous function as an argument and assign it to the ```server``` variable.
3030

31-
```JavaScript
32-
var http = require('http');
31+
```JavaScript
32+
var http = require('http');
3333
34-
var server = http.createServer(function () { });
35-
```
34+
var server = http.createServer(function () { });
35+
```
3636

3737
4. This server is not yet bound to any port. In order to bind to a port, the [server](http://nodejs.org/api/http.html#http_class_http_server) object has the function ```server.listen(port)``` that takes a port as the first argument.
3838

39-
```JavaScript
40-
var http = require('http');
39+
```JavaScript
40+
var http = require('http');
4141
42-
var server = http.createServer(function () { });
43-
44-
server.listen(8080);
45-
```
42+
var server = http.createServer(function () { });
43+
44+
server.listen(8080);
45+
```
4646
5. Launch your server at the command line: ```node server.js```
4747

4848
6. Open your browser and navigate to ```http://localhost:8080``` (replace 8080 with whatever port you chose if different). You will notice that your browser seems to hang and will eventually timeout. This is because our server is not yet doing anything useful with the incoming connection. Let's start by responding to the request with a 200 HTTP status code.
4949
50-
Here's where we are so far.
51-
52-
```JavaScript
53-
var http = require('http');
54-
55-
var server = http.createServer(function (req, res) {
56-
res.statusCode = 200;
57-
res.end();
58-
});
59-
60-
server.listen(8080);
61-
```
50+
Here's where we are so far.
51+
52+
```JavaScript
53+
var http = require('http');
54+
55+
var server = http.createServer(function (req, res) {
56+
res.statusCode = 200;
57+
res.end();
58+
});
59+
60+
server.listen(8080);
61+
```
6262

63-
Notice we have added a few arguments to the anonymous function ```req``` and ```res```. These represent the [request](http://nodejs.org/api/http.html#http_class_http_serverrequest) and [response](http://nodejs.org/api/http.html#http_class_http_serverresponse) streams respectively.
63+
Notice we have added a few arguments to the anonymous function ```req``` and ```res```. These represent the [request](http://nodejs.org/api/http.html#http_class_http_serverrequest) and [response](http://nodejs.org/api/http.html#http_class_http_serverresponse) streams respectively.
6464

65-
A call to ```res.end()``` is required in order to let the client know the server has finished the response.
65+
A call to ```res.end()``` is required in order to let the client know the server has finished the response.
6666

6767
7. Visit ```http://localhost:8080``` once again. This time there should be a page with no content. We are not here to serve blank pages so let's actually output some content. The response stream (```res``` has a ```write``` function that takes a string to write to the output.
6868
69-
```JavaScript
70-
var http = require('http');
69+
```JavaScript
70+
var http = require('http');
7171
72-
var server = http.createServer(function (req, res) {
73-
res.statusCode = 200;
74-
res.write('Hello World!');
75-
res.end();
76-
});
72+
var server = http.createServer(function (req, res) {
73+
res.statusCode = 200;
74+
res.write('Hello World!');
75+
res.end();
76+
});
7777
78-
server.listen(8080);
79-
```
80-
78+
server.listen(8080);
79+
```
80+
8181
### Writing the content of a file to the response
8282
8383
1. To load a files content from disk:
8484
85-
```JavaScript
86-
var fs = require('fs');
85+
```JavaScript
86+
var fs = require('fs');
8787
88-
fs.readFile('index.html', function (err, data) {
89-
if (!err) {
90-
console.log(data);
91-
}
92-
});
88+
fs.readFile('index.html', function (err, data) {
89+
if (!err) {
90+
console.log(data);
91+
}
92+
});
9393
94-
```
94+
```
9595
9696
1. This won't work because theres no ```index.html``` in our directory. Let's create that with something like this:
9797
98-
```HTML
99-
<html>
100-
<head>
101-
<title>My Node.JS server</title>
102-
</head>
103-
<body>
104-
<h1>Hello World!</h1>
105-
</body>
106-
</html>
107-
```
98+
```HTML
99+
<html>
100+
<head>
101+
<title>My Node.JS server</title>
102+
</head>
103+
<body>
104+
<h1>Hello World!</h1>
105+
</body>
106+
</html>
107+
```
108108
109109
1. Now that we know how to read a file from disk let's join that with our previous HTTP server example.
110110

111-
```JavaScript
112-
var fs = require('fs');
113-
var http = require('http');
111+
```JavaScript
112+
var fs = require('fs');
113+
var http = require('http');
114114
115-
var server = http.createServer(function (req, res) {
116-
res.statusCode = 200;
115+
var server = http.createServer(function (req, res) {
116+
res.statusCode = 200;
117117
118-
fs.readFile('index.html', function (err, data) {
119-
if (!err) {
120-
res.write(data);
121-
res.end();
122-
}
123-
});
124-
});
118+
fs.readFile('index.html', function (err, data) {
119+
if (!err) {
120+
res.write(data);
121+
res.end();
122+
}
123+
});
124+
});
125125
126-
server.listen(8080);
127-
```
126+
server.listen(8080);
127+
```
128128

129129
### A simple template engine
130130

@@ -133,25 +133,25 @@ In this lab we will create an HTTP server that responds to all requests with a s
133133
```JavaScript
134134
var templateEngine = function (template, data) {
135135
136-
var vars = template.match(/\{\w+\}/g);
136+
var vars = template.match(/\{\w+\}/g);
137137
138-
if (vars === null) {
139-
return template;
140-
}
138+
if (vars === null) {
139+
return template;
140+
}
141141
142-
var nonVars = template.split(/\{\w+\}/g);
143-
var output = '';
142+
var nonVars = template.split(/\{\w+\}/g);
143+
var output = '';
144144
145-
for (var i = 0; i < nonVars.length; i++) {
146-
output += nonVars[i];
145+
for (var i = 0; i < nonVars.length; i++) {
146+
output += nonVars[i];
147147
148-
if (i < vars.length) {
149-
var key = vars[i].replace(/[\{\}]/g, '');
150-
output += data[key]
151-
}
152-
}
148+
if (i < vars.length) {
149+
var key = vars[i].replace(/[\{\}]/g, '');
150+
output += data[key]
151+
}
152+
}
153153
154-
return output;
154+
return output;
155155
};
156156
```
157157
@@ -165,36 +165,36 @@ var http = require('http');
165165
166166
var templateEngine = function (template, data) {
167167
168-
var vars = template.match(/\{\w+\}/g);
169-
170-
if (vars === null) {
171-
return template;
172-
}
173-
174-
var nonVars = template.split(/\{\w+\}/g);
175-
var output = '';
176-
177-
for (var i = 0; i < nonVars.length; i++) {
178-
output += nonVars[i];
179-
180-
if (i < vars.length) {
181-
var key = vars[i].replace(/[\{\}]/g, '');
182-
output += data[key]
183-
}
184-
}
185-
186-
return output;
168+
var vars = template.match(/\{\w+\}/g);
169+
170+
if (vars === null) {
171+
return template;
172+
}
173+
174+
var nonVars = template.split(/\{\w+\}/g);
175+
var output = '';
176+
177+
for (var i = 0; i < nonVars.length; i++) {
178+
output += nonVars[i];
179+
180+
if (i < vars.length) {
181+
var key = vars[i].replace(/[\{\}]/g, '');
182+
output += data[key]
183+
}
184+
}
185+
186+
return output;
187187
};
188188
189189
var server = http.createServer(function (req, res) {
190-
res.statusCode = 200;
191-
192-
fs.readFile('index.html', function (err, data) {
193-
if (!err) {
194-
res.write(templateEngine(data, {})); // use our template engine here
195-
res.end();
196-
}
197-
});
190+
res.statusCode = 200;
191+
192+
fs.readFile('index.html', function (err, data) {
193+
if (!err) {
194+
res.write(templateEngine(data, {})); // use our template engine here
195+
res.end();
196+
}
197+
});
198198
});
199199
200200
server.listen(8080);
@@ -204,36 +204,36 @@ Now try this in the browser. You'll notice that the output is the same. Let's up
204204

205205
```HTML
206206
<html>
207-
<head>
208-
<title>My Node.JS server</title>
209-
</head>
210-
<body>
211-
<h1>Hello {name}!</h1>
212-
<ul>
213-
<li>Node Version: {node}</li>
214-
<li>V8 Version: {v8}</li>
215-
<li>URL: {url}</li>
216-
<li>Time: {time}</li>
217-
</ul>
218-
</body>
207+
<head>
208+
<title>My Node.JS server</title>
209+
</head>
210+
<body>
211+
<h1>Hello {name}!</h1>
212+
<ul>
213+
<li>Node Version: {node}</li>
214+
<li>V8 Version: {v8}</li>
215+
<li>URL: {url}</li>
216+
<li>Time: {time}</li>
217+
</ul>
218+
</body>
219219
</html>
220220
```
221-
221+
222222
The above modifications require several properties on our data object (name, node, v8, url, time), let's assign those:
223223
224224
```JavaScript
225225
... (code omitted from example)
226226
fs.readFile('index.html', function (err, data) {
227-
if(!err) {
228-
res.write(templateEngine(data, {
229-
name: 'Ryan Dahl',
230-
node: process.versions.node,
231-
v8: process.versions.v8,
232-
time: new Date(),
233-
url: req.url
234-
}));
235-
res.end();
236-
}
227+
if(!err) {
228+
res.write(templateEngine(data, {
229+
name: 'Ryan Dahl',
230+
node: process.versions.node,
231+
v8: process.versions.v8,
232+
time: new Date(),
233+
url: req.url
234+
}));
235+
res.end();
236+
}
237237
});
238238
... (code omitted from example)
239239
```

0 commit comments

Comments
 (0)