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
In this lab we will create an HTTP server that responds to all requests with a simple HTML template.
21
21
22
22
1. Create a new file named ```server.js``` in a directory of your choice.
23
23
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
+
```
28
28
29
29
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.
30
30
31
-
```JavaScript
32
-
var http =require('http');
31
+
```JavaScript
32
+
var http = require('http');
33
33
34
-
var server =http.createServer(function () { });
35
-
```
34
+
var server = http.createServer(function () { });
35
+
```
36
36
37
37
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.
38
38
39
-
```JavaScript
40
-
var http =require('http');
39
+
```JavaScript
40
+
var http = require('http');
41
41
42
-
var server =http.createServer(function () { });
43
-
44
-
server.listen(8080);
45
-
```
42
+
var server = http.createServer(function () { });
43
+
44
+
server.listen(8080);
45
+
```
46
46
5. Launch your server at the command line:```node server.js```
47
47
48
48
6. Open your browser and navigate to ```http://localhost:8080``` (replace 8080with 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.
49
49
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
+
```
62
62
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.
64
64
65
-
A call to ```res.end()``` is required in order to let the client know the server has finished the response.
7.Visit```http://localhost:8080```onceagain. Thistimethereshouldbeapagewithnocontent. Wearenotheretoserveblankpagessolet's actually output some content. The response stream (```res``` has a ```write``` function that takes a string to write to the output.
68
68
69
-
```JavaScript
70
-
var http =require('http');
69
+
```JavaScript
70
+
var http = require('http');
71
71
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('HelloWorld!');
75
+
res.end();
76
+
});
77
77
78
-
server.listen(8080);
79
-
```
80
-
78
+
server.listen(8080);
79
+
```
80
+
81
81
### Writing the content of a file to the response
82
82
83
83
1. To load a files content from disk:
84
84
85
-
```JavaScript
86
-
var fs =require('fs');
85
+
```JavaScript
86
+
var fs = require('fs');
87
87
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
+
});
93
93
94
-
```
94
+
```
95
95
96
96
1. This won'tworkbecausetheresno```index.html```inourdirectory. Let's create that with something like this:
97
97
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
+
```
108
108
109
109
1. Now that we know how to read a file from disk let'sjointhatwithourpreviousHTTPserverexample.
110
110
111
-
```JavaScript
112
-
var fs =require('fs');
113
-
var http =require('http');
111
+
```JavaScript
112
+
var fs = require('fs');
113
+
var http = require('http');
114
114
115
-
var server =http.createServer(function (req, res) {
116
-
res.statusCode=200;
115
+
var server = http.createServer(function (req, res) {
116
+
res.statusCode = 200;
117
117
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
+
});
125
125
126
-
server.listen(8080);
127
-
```
126
+
server.listen(8080);
127
+
```
128
128
129
129
### Asimpletemplateengine
130
130
@@ -133,25 +133,25 @@ In this lab we will create an HTTP server that responds to all requests with a s
133
133
```JavaScript
134
134
var templateEngine = function (template, data) {
135
135
136
-
var vars =template.match(/\{\w+\}/g);
136
+
var vars = template.match(/\{\w+\}/g);
137
137
138
-
if (vars ===null) {
139
-
return template;
140
-
}
138
+
if (vars === null) {
139
+
return template;
140
+
}
141
141
142
-
var nonVars =template.split(/\{\w+\}/g);
143
-
var output ='';
142
+
var nonVars = template.split(/\{\w+\}/g);
143
+
var output = '';
144
144
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];
147
147
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
+
}
153
153
154
-
return output;
154
+
return output;
155
155
};
156
156
```
157
157
@@ -165,36 +165,36 @@ var http = require('http');
165
165
166
166
var templateEngine = function (template, data) {
167
167
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;
187
187
};
188
188
189
189
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
+
});
198
198
});
199
199
200
200
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
0 commit comments