Permalink
Browse files

POST request handle

  • Loading branch information...
zzagtung committed Dec 13, 2011
1 parent bac36ba commit 18b5b5ed026ce1ad86fe6fde2089bed7c114880a
Showing with 16 additions and 7 deletions.
  1. +4 −4 requestHandlers.js
  2. +2 −2 router.js
  3. +10 −1 server.js
View
@@ -1,6 +1,6 @@
-var exec = require("child_process").exec;
+var querystring = require("querystring");
-function start(response) {
+function start(response, postData) {
console.log("Request handler 'start' was called.");
var body = '<html>'+
'<head>'+
@@ -19,10 +19,10 @@ function start(response) {
response.end();
}
-function upload(response) {
+function upload(response, postData) {
console.log("Request handler 'upload' was called.");
response.writeHead(200, {"Content-Type": "text/plain"});
- response.write("Hello Upload");
+ response.write("You've sent the text: " + querystring.parse(postData).text);
response.end();
}
View
@@ -1,7 +1,7 @@
-function route(handle, pathname, response) {
+function route(handle, pathname, response, postData) {
console.log("About to route a request for " + pathname);
if (typeof handle[pathname] === 'function') {
- return handle[pathname](response);
+ return handle[pathname](response, postData);
}
else {
console.log("No request handler found for " + pathname);
View
@@ -3,10 +3,19 @@ var url = require("url");
function start(route, handle) {
function onReqeust(request, response) {
+ var postData = "";
var pathname = url.parse(request.url).pathname;
console.log("Reqeust for " + pathname + " received.");
+
+ request.setEncoding("utf8");
- route(handle, pathname, response);
+ request.addListener("data", function(postDataChunk) {
+ postData += postDataChunk;
+ console.log("Received POST data chunk '" + postDataChunk + "'.");
+ });
+ request.addListener("end", function() {
+ route(handle, pathname, response, postData);
+ });
}
http.createServer(onReqeust).listen("8888");

0 comments on commit 18b5b5e

Please sign in to comment.