Skip to content

Commit e8adc5a

Browse files
committed
Adopted Markdown for Basic HTTP Client & Server
1 parent 0a4b704 commit e8adc5a

File tree

3 files changed

+35
-32
lines changed

3 files changed

+35
-32
lines changed

authors.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ The following people are totally rad and awesome because they have contributed r
1616
* James C. Holder *[email protected]*
1717
* Jason Giedymin *[email protected]*
1818
* Phil Cohen *[email protected]*
19+
* João Moreno *coffeecb @joaomoreno .com*
1920
* ...You! What are you waiting for? Check out the [contributing](/contributing) section and get cracking!
2021

2122
# Developers

chapters/networking/basic-http-client.textile renamed to chapters/networking/basic-http-client.md

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,15 @@ title: Basic HTTP Client
44
chapter: Networking
55
---
66

7-
h2. Problem
7+
## Problem
88

99
You want to create a HTTP client.
1010

11-
h2. Solution
11+
## Solution
1212

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.
1414

15-
h3. GET something
15+
### GET something
1616

1717
{% highlight coffeescript %}
1818
http = require 'http'
@@ -21,17 +21,17 @@ http.get { host: 'www.google.com' }, (res) ->
2121
console.log res.statusCode
2222
{% endhighlight %}
2323

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:
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:
2525

2626
{% highlight console %}
2727
$ coffee http-client.coffee
2828
200
2929

3030
{% endhighlight %}
3131

32-
h3. What's my IP?
32+
### What's my IP?
3333

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.
3535

3636
{% highlight coffeescript %}
3737
http = require 'http'
@@ -44,20 +44,20 @@ http.get { host: 'checkip.dyndns.org' }, (res) ->
4444
console.log data.match(/([0-9]+\.){3}[0-9]+/)[0]
4545
{% endhighlight %}
4646

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:
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:
4848

4949
{% highlight console %}
5050
$ coffee http-client.coffee
5151
123.123.123.123
5252
{% endhighlight %}
5353

54-
h2. Discussion
54+
## Discussion
5555

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.
5757

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.
5959

60-
h3. Exercises
60+
### Exercises
6161

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.
6363

chapters/networking/basic-http-server.textile renamed to chapters/networking/basic-http-server.md

Lines changed: 21 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -4,25 +4,25 @@ title: Basic HTTP Server
44
chapter: Networking
55
---
66

7-
h2. Problem
7+
## Problem
88

99
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.
1010

11-
h2. Solution
11+
## Solution
1212

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.
1414

15-
h3. Say 'hi\n'
15+
### Say 'hi\n'
1616

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.
1818

1919
{% highlight coffeescript %}
2020
http = require 'http'
2121
server = http.createServer (req, res) -> res.end 'hi\n'
2222
server.listen 8000
2323
{% endhighlight %}
2424

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:
2626

2727
{% highlight console %}
2828
$ curl -D - http://localhost:8000/
@@ -33,7 +33,7 @@ Transfer-Encoding: chunked
3333
hi
3434
{% endhighlight %}
3535

36-
h3. What's going on?
36+
### What's going on?
3737

3838
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.
3939

@@ -51,7 +51,7 @@ server = http.createServer (req, res) ->
5151
server.listen 8000
5252
{% endhighlight %}
5353

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:
5555

5656
{% highlight console %}
5757
$ coffee http-server.coffee
@@ -60,9 +60,9 @@ GET /coffee
6060
GET /user/1337
6161
{% endhighlight %}
6262

63-
h3. GETting stuff
63+
### GETting stuff
6464

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.
6666

6767
{% highlight coffeescript %}
6868
http = require 'http'
@@ -89,6 +89,8 @@ server = http.createServer (req, res) ->
8989
server.listen 8000
9090
{% endhighlight %}
9191

92+
We can try several URLs to see how it responds:
93+
9294
{% highlight console %}
9395
$ curl -D - http://localhost:8000/coffee
9496
HTTP/1.1 200 OK
@@ -105,9 +107,9 @@ Transfer-Encoding: chunked
105107

106108
{% endhighlight %}
107109

108-
h3. Use your head(ers)
110+
### Use your head(ers)
109111

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:
111113

112114
{% highlight coffeescript %}
113115
http = require 'http'
@@ -149,7 +151,7 @@ server = http.createServer (req, res) ->
149151
server.listen 8000
150152
{% endhighlight %}
151153

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:
153155

154156
{% highlight console %}
155157
$ curl http://localhost:8000/
@@ -166,7 +168,7 @@ $ curl -H "Accept: image/png" http://localhost:8000/coffee
166168
Unknown format
167169
{% endhighlight %}
168170

169-
h3. You gotta give to get back
171+
### You gotta give to get back
170172

171173
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.
172174

@@ -232,7 +234,7 @@ server = http.createServer (req, res) ->
232234
server.listen 8000
233235
{% endhighlight %}
234236

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`.
236238

237239
{% highlight console %}
238240
$ curl -D - http://localhost:8000/cookie
@@ -247,13 +249,13 @@ HTTP/1.1 200 OK # ...
247249
{"key":"cookie","value":"monster"}
248250
{% endhighlight %}
249251

250-
h2. Discussion
252+
## Discussion
251253

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`.
253255

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.
255257

256-
h3. Exercises
258+
### Exercises
257259

258260
* Create a layer in between the server and the developer which would allow the developer to do something like:
259261

0 commit comments

Comments
 (0)