Permalink
Browse files

final code examples working

  • Loading branch information...
1 parent 6a2998c commit 562f67f4e1bf939019b7f4da64314fc478b8eefb @manuelkiessling committed Jun 27, 2011
Showing with 37 additions and 46 deletions.
  1. +11 −11 code/application/requestHandlers.js
  2. +2 −2 code/application/router.js
  3. +2 −14 code/application/server.js
  4. +22 −19 index.html
@@ -3,7 +3,7 @@ var querystring = require("querystring"),
formidable = require("formidable"),
sys = require("sys");
-function start(response, postData) {
+function start(response) {
console.log("Request handler 'start' was called.");
var body = '<html>'+
@@ -23,20 +23,20 @@ function start(response, postData) {
response.end();
}
-function upload(response, postData, request) {
+function upload(response, request) {
console.log("Request handler 'upload' was called.");
- var form = new formidable.IncomingForm();
- console.log("about to parse");
- form.parse(request, function(err, fields, files) {
- console.log("parsing done");
- response.writeHead(200, {'content-type': 'text/plain'});
- response.write('received upload:\n\n');
- response.end(sys.inspect({fields: fields, files: files}));
- });
+ var form = new formidable.IncomingForm();
+ console.log("about to parse");
+ form.parse(request, function(err, fields, files) {
+ console.log("parsing done");
+ response.writeHead(200, {'content-type': 'text/html'});
+ response.write('received upload:<br/>');
+ response.end(sys.inspect({fields: fields, files: files}));
+ });
}
-function show(response, postData) {
+function show(response) {
console.log("Request handler 'show' was called.");
fs.readFile("/tmp/test.png", "binary", function(error, file) {
if(error) {
@@ -1,7 +1,7 @@
-function route(handle, pathname, response, postData, request) {
+function route(handle, pathname, response, request) {
console.log("About to route a request for " + pathname);
if (typeof handle[pathname] === 'function') {
- handle[pathname](response, postData, request);
+ handle[pathname](response, request);
} else {
console.log("No request handler found for " + pathname);
response.writeHead(404, {"Content-Type": "text/html"});
@@ -3,21 +3,9 @@ var url = require("url");
function start(route, handle) {
function onRequest(request, response) {
- var postData = "";
- var pathname = url.parse(request.url).pathname;
+ var pathname = url.parse(request.url).pathname;
console.log("Request for " + pathname + " received.");
-
- request.setEncoding("utf8");
-
- request.addListener("data", function(postDataChunk) {
- postData += postDataChunk;
- console.log("Received POST data chunk '" + postDataChunk + "'.");
- });
-
- request.addListener("end", function() {
- route(handle, pathname, response, postData, request);
- });
-
+ route(handle, pathname, response, request);
}
http.createServer(onRequest).listen(8888);
View
@@ -2288,7 +2288,7 @@
function start(route, handle) {
function onRequest(request, response) {
var postData = "";
- var pathname = url.parse(request.url).pathname;
+ var pathname = url.parse(request.url).pathname;
console.log("Request for " + pathname + " received.");
request.setEncoding("utf8");
@@ -2732,28 +2732,30 @@
to pass the <em>request</em> object all the way from the server to
the router to the request handler. There may be more elegant
solutions, but this approach should do the job for now.
- Let's start with <em>server.js</em>:
+ </p>
+ <p>
+ And while we are at it, let's remove the whole postData stuff in
+ our server and request handlers - we won't need it for handling the
+ file upload, and it even raises a problem: we already "consumed"
+ the <em>data</em> events of the <em>request</em> object in the
+ server, which means that <em>form.parse</em>, which also needs to
+ consume those events, wouldn't receive any more data from them
+ (because Node.js doesn't buffer any data).
+ </p>
+ <p>
+ Let's start with <em>server.js</em> - we remove the postData
+ handling and the <em>request.setEncoding</em> line (which is going
+ to be handled by node-formidable itself):
+
</p>
<pre class="prettyprint lang-js">var http = require("http");
var url = require("url");
function start(route, handle) {
function onRequest(request, response) {
- var postData = "";
- var pathname = url.parse(request.url).pathname;
+ var pathname = url.parse(request.url).pathname;
console.log("Request for " + pathname + " received.");
-
- request.setEncoding("utf8");
-
- request.addListener("data", function(postDataChunk) {
- postData += postDataChunk;
- console.log("Received POST data chunk '" + postDataChunk + "'.");
- });
-
- request.addListener("end", function() {
- route(handle, pathname, response, postData, request);
- });
-
+ route(handle, pathname, response, request);
}
http.createServer(onRequest).listen(8888);
@@ -2764,12 +2766,13 @@
</pre>
<p>
- Next comes <em>router.js</em>:
+ Next comes <em>router.js</em> - we don't need to pass <em>postData</em>
+ on anymore, and instead pass <em>request</em>:
</p>
-<pre class="prettyprint lang-js">function route(handle, pathname, response, postData, request) {
+<pre class="prettyprint lang-js">function route(handle, pathname, response, request) {
console.log("About to route a request for " + pathname);
if (typeof handle[pathname] === 'function') {
- handle[pathname](response, postData, request);
+ handle[pathname](response, request);
} else {
console.log("No request handler found for " + pathname);
response.writeHead(404, {"Content-Type": "text/html"});

0 comments on commit 562f67f

Please sign in to comment.