Skip to content

Commit 5b93328

Browse files
committed
Updated http module lab
1 parent a6b4364 commit 5b93328

File tree

1 file changed

+25
-3
lines changed

1 file changed

+25
-3
lines changed

_posts/2012-11-3-http.md

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,15 +41,15 @@ var server = http.createServer(function (req, res) {
4141
server.listen(8080);
4242
{% endhighlight %}
4343

44-
Let's boot up our server and take a look. Run the script:
44+
Let's boot up our server and take a look:
4545

4646
{% highlight bash %}
4747
$ node server.js
4848
{% endhighlight %}
4949

5050
Now, open your browser and navigate to [http://localhost:8080](http://localhost:8080). 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.
5151

52-
Let's start by responding to all requests with a 200 status code. This is HTTP speak for everything is OK.
52+
Let's start by responding to all requests with a 200 status code. This is HTTP speak for "everything is OK.""
5353

5454
{% highlight javascript %}
5555
var http = require('http');
@@ -229,4 +229,26 @@ fs.readFile('index.html', function (err, data) {
229229
// ... code omitted for brevity
230230
{% endhighlight %}
231231

232-
In case you haven't noticed, nowhere have we written any logic that returns different data depending on the path of the request. As such, our simple HTTP server responds to every path the same. For example: [http://localhost:8080/path/to/file/one](http://localhost:8080/path/to/file/one) and [http://localhost:8080/path/to/file/two](http://localhost:8080/path/to/file/two).
232+
## On Your Own
233+
234+
In case you haven't noticed, nowhere have we written any logic that returns different data depending on the path of the request. As such, our simple HTTP server responds to every path the same. For example: [http://localhost:8080/path/to/file/one](http://localhost:8080/path/to/file/one) and [http://localhost:8080/path/to/file/two](http://localhost:8080/path/to/file/two). Experiment with `req.url` to see if you can route requests based on the resource requested. Another useful module is [url](http://nodejs.org/api/url.html).
235+
236+
Here's an example usage of the url module:
237+
238+
{% highlight javascript %}
239+
> var url = require('url')
240+
undefined
241+
> url.parse('http://localhost:8080/path/to/file?q=1')
242+
{ protocol: 'http:',
243+
slashes: true,
244+
auth: null,
245+
host: 'localhost:8080',
246+
port: '8080',
247+
hostname: 'localhost',
248+
hash: null,
249+
search: '?q=1',
250+
query: 'q=1',
251+
pathname: '/path/to/file',
252+
path: '/path/to/file?q=1',
253+
href: 'http://localhost:8080/path/to/file?q=1' }
254+
{% endhighlight %}

0 commit comments

Comments
 (0)