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: _posts/2012-11-3-http.md
+25-3Lines changed: 25 additions & 3 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -41,15 +41,15 @@ var server = http.createServer(function (req, res) {
41
41
server.listen(8080);
42
42
{% endhighlight %}
43
43
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:
45
45
46
46
{% highlight bash %}
47
47
$ node server.js
48
48
{% endhighlight %}
49
49
50
50
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.
51
51
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.""
53
53
54
54
{% highlight javascript %}
55
55
var http = require('http');
@@ -229,4 +229,26 @@ fs.readFile('index.html', function (err, data) {
229
229
// ... code omitted for brevity
230
230
{% endhighlight %}
231
231
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).
0 commit comments