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: chapters/networking/basic-http-client.md
+13-13Lines changed: 13 additions & 13 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -4,15 +4,15 @@ title: Basic HTTP Client
4
4
chapter: Networking
5
5
---
6
6
7
-
h2. Problem
7
+
##Problem
8
8
9
9
You want to create a HTTP client.
10
10
11
-
h2. Solution
11
+
##Solution
12
12
13
-
In this recipe, we'll use "node.js":http://nodejs.org/'s HTTP library. We'll go from a simple GET request example to a client which returns the external IP of a computer.
13
+
In this recipe, we'll use [node.js](http://nodejs.org/)'s HTTP library. We'll go from a simple GET request example to a client which returns the external IP of a computer.
The @get@ function, from node.js's @http@ module, issues a GET request to a HTTP server. The response comes in the form of a callback, which we can handle in a function. This example merely prints the response status code. Check it out:
24
+
The `get` function, from node.js's `http` module, issues a GET request to a HTTP server. The response comes in the form of a callback, which we can handle in a function. This example merely prints the response status code. Check it out:
25
25
26
26
{% highlight console %}
27
27
$ coffee http-client.coffee
28
28
200
29
29
30
30
{% endhighlight %}
31
31
32
-
h3. What's my IP?
32
+
###What's my IP?
33
33
34
-
If you are inside a network which relies on "NAT":http://en.wikipedia.org/wiki/Network_address_translation such as a LAN, you probably have faced the issue of finding out what's your external IP address. Let's write a small coffeescript for this.
34
+
If you are inside a network which relies on [NAT](http://en.wikipedia.org/wiki/Network_address_translation) such as a LAN, you probably have faced the issue of finding out what's your external IP address. Let's write a small coffeescript for this.
We can get the data from the result object by listening on its @'data'@ event; and know that it has come to an end once the @'end'@ event has been fired. When that happens, we can do a simple regular expression match to extract our IP address. Try it:
47
+
We can get the data from the result object by listening on its `'data'` event; and know that it has come to an end once the `'end'` event has been fired. When that happens, we can do a simple regular expression match to extract our IP address. Try it:
48
48
49
49
{% highlight console %}
50
50
$ coffee http-client.coffee
51
51
123.123.123.123
52
52
{% endhighlight %}
53
53
54
-
h2. Discussion
54
+
##Discussion
55
55
56
-
Note that @http.get@ is a shortcut of @http.request@. The latter allows you to issue HTTP requests with different methods, such as POST or PUT.
56
+
Note that `http.get` is a shortcut of `http.request`. The latter allows you to issue HTTP requests with different methods, such as POST or PUT.
57
57
58
-
For API and overall information on this subject, check node.js's "http":http://nodejs.org/docs/latest/api/http.html and "https":http://nodejs.org/docs/latest/api/https.html documentation pages. Also, the "HTTP spec":http://www.ietf.org/rfc/rfc2616.txt might come in handy.
58
+
For API and overall information on this subject, check node.js's [http](http://nodejs.org/docs/latest/api/http.html) and [https](http://nodejs.org/docs/latest/api/https.html) documentation pages. Also, the [HTTP spec](http://www.ietf.org/rfc/rfc2616.txt) might come in handy.
59
59
60
-
h3. Exercises
60
+
###Exercises
61
61
62
-
* Create a client for the key-value store HTTP server, from the "Basic HTTP Server":http://coffeescriptcookbook.com/chapters/networking/basic-http-server recipe.
62
+
* Create a client for the key-value store HTTP server, from the [Basic HTTP Server](basic-http-server) recipe.
Copy file name to clipboardExpand all lines: chapters/networking/basic-http-server.md
+21-19Lines changed: 21 additions & 19 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -4,25 +4,25 @@ title: Basic HTTP Server
4
4
chapter: Networking
5
5
---
6
6
7
-
h2. Problem
7
+
##Problem
8
8
9
9
You want to create a HTTP server over a network. Over the course of this recipe, we'll go step by step from the smallest server possible to a functional key-value store.
10
10
11
-
h2. Solution
11
+
##Solution
12
12
13
-
We'll use "node.js":http://nodejs.org/'s HTTP library to our own selfish purposes and create the simplest web server possible in Coffeescript.
13
+
We'll use [node.js](http://nodejs.org/)'s HTTP library to our own selfish purposes and create the simplest web server possible in Coffeescript.
14
14
15
-
h3. Say 'hi\n'
15
+
###Say 'hi\n'
16
16
17
-
We can start by importing the @http@ module. This module has a nice helper function — @createServer@ — which, given a simple request handler, creates a HTTP server. All that's left to do then is have the server listening on a port.
17
+
We can start by importing node.js's HTTP module. This contains `createServer`which, given a simple request handler, returns a HTTP server. We can use that server to listen on a TCP port.
18
18
19
19
{% highlight coffeescript %}
20
20
http = require 'http'
21
21
server = http.createServer (req, res) -> res.end 'hi\n'
22
22
server.listen 8000
23
23
{% endhighlight %}
24
24
25
-
To run this example, simply put in a file and run it. You can kill it with @Ctrl-C@. We can test it using the @curl@ command, available on most *nix platforms:
25
+
To run this example, simply put in a file and run it. You can kill it with `Ctrl-C`. We can test it using the `curl` command, available on most \*nix platforms:
26
26
27
27
{% highlight console %}
28
28
$ curl -D - http://localhost:8000/
@@ -33,7 +33,7 @@ Transfer-Encoding: chunked
33
33
hi
34
34
{% endhighlight %}
35
35
36
-
h3. What's going on?
36
+
###What's going on?
37
37
38
38
Let's get a little bit more feedback on what's happening on our server. While we're at it, we could also be friendlier to our clients and provide them some HTTP headers.
39
39
@@ -51,7 +51,7 @@ server = http.createServer (req, res) ->
51
51
server.listen 8000
52
52
{% endhighlight %}
53
53
54
-
Try to access it once again, but this time use different URL paths, such as @http://localhost:8000/coffee@. You'll see something like this on the server console:
54
+
Try to access it once again, but this time use different URL paths, such as `http://localhost:8000/coffee`. You'll see something like this on the server console:
55
55
56
56
{% highlight console %}
57
57
$ coffee http-server.coffee
@@ -60,9 +60,9 @@ GET /coffee
60
60
GET /user/1337
61
61
{% endhighlight %}
62
62
63
-
h3. GETting stuff
63
+
###GETting stuff
64
64
65
-
What if our webserver was able to hold some data? We'll try to come up with a simple key-value store in which elements are retrievable via GET requests. Provide a key on the request path and the server will return the corresponding value - or 404 if it doesn't exist.
65
+
What if our webserver was able to hold some data? We'll try to come up with a simple key-value store in which elements are retrievable via GET requests. Provide a key on the request path and the server will return the corresponding value — or 404 if it doesn't exist.
66
66
67
67
{% highlight coffeescript %}
68
68
http = require 'http'
@@ -89,6 +89,8 @@ server = http.createServer (req, res) ->
89
89
server.listen 8000
90
90
{% endhighlight %}
91
91
92
+
We can try several URLs to see how it responds:
93
+
92
94
{% highlight console %}
93
95
$ curl -D - http://localhost:8000/coffee
94
96
HTTP/1.1 200 OK
@@ -105,9 +107,9 @@ Transfer-Encoding: chunked
105
107
106
108
{% endhighlight %}
107
109
108
-
h3. Use your head(ers)
110
+
###Use your head(ers)
109
111
110
-
Let's face it, @text/plain@ is kind of lame. How about if we use something hip like @application/json@ or @text/xml@? Also, our store retrieval process could use a bit of refactoring — how about some exception throwing & handling? Let's see what we can come up with:
112
+
Let's face it, `text/plain` is kind of lame. How about if we use something hip like `application/json` or `text/xml`? Also, our store retrieval process could use a bit of refactoring — how about some exception throwing & handling? Let's see what we can come up with:
111
113
112
114
{% highlight coffeescript %}
113
115
http = require 'http'
@@ -149,7 +151,7 @@ server = http.createServer (req, res) ->
149
151
server.listen 8000
150
152
{% endhighlight %}
151
153
152
-
This server will still return the value which matches a given key, or 404 if non-existent. But it will structure the response either in JSON or XML, according to the @Accept@ header. See for yourself:
154
+
This server will still return the value which matches a given key, or 404 if non-existent. But it will structure the response either in JSON or XML, according to the `Accept` header. See for yourself:
The obvious last step in our adventure is to provide the client the ability to store data. We'll keep our RESTiness by listening to POST requests for this purpose.
172
174
@@ -232,7 +234,7 @@ server = http.createServer (req, res) ->
232
234
server.listen 8000
233
235
{% endhighlight %}
234
236
235
-
Notice how the data is received in a POST request. By attaching some handlers on the @'data'@ and @'end'@ events of the request object, we're able to buffer and finally save the data from the client in the @store@.
237
+
Notice how the data is received in a POST request. By attaching some handlers on the `'data'` and `'end'` events of the request object, we're able to buffer and finally save the data from the client in the `store`.
236
238
237
239
{% highlight console %}
238
240
$ curl -D - http://localhost:8000/cookie
@@ -247,13 +249,13 @@ HTTP/1.1 200 OK # ...
247
249
{"key":"cookie","value":"monster"}
248
250
{% endhighlight %}
249
251
250
-
h2. Discussion
252
+
##Discussion
251
253
252
-
Give @http.createServer@ a function in the shape of @(request, response) -> ...@ and it will return a server object, which we can use to listen on a port. Interact with the @request@ and @response@ objects to give the server its behaviour. Listen on port 8000 using @server.listen 8000@.
254
+
Give `http.createServer` a function in the shape of `(request, response) -> ...` and it will return a server object, which we can use to listen on a port. Interact with the `request` and `response` objects to give the server its behaviour. Listen on port 8000 using `server.listen 8000`.
253
255
254
-
For API and overall information on this subject, check node.js's "http":http://nodejs.org/docs/latest/api/http.html and "https":http://nodejs.org/docs/latest/api/https.html documentation pages. Also, the "HTTP spec":http://www.ietf.org/rfc/rfc2616.txt might come in handy.
256
+
For API and overall information on this subject, check node.js's [http](http://nodejs.org/docs/latest/api/http.html) and [https](http://nodejs.org/docs/latest/api/https.html) documentation pages. Also, the [HTTP spec](http://www.ietf.org/rfc/rfc2616.txt) might come in handy.
255
257
256
-
h3. Exercises
258
+
###Exercises
257
259
258
260
* Create a layer in between the server and the developer which would allow the developer to do something like:
0 commit comments