Skip to content

Commit

Permalink
POST request handle
Browse files Browse the repository at this point in the history
  • Loading branch information
zzagtung committed Dec 13, 2011
1 parent bac36ba commit 18b5b5e
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 7 deletions.
8 changes: 4 additions & 4 deletions requestHandlers.js
Original file line number Diff line number Diff line change
@@ -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>'+
Expand All @@ -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();
}

Expand Down
4 changes: 2 additions & 2 deletions router.js
Original file line number Diff line number Diff line change
@@ -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);
Expand Down
11 changes: 10 additions & 1 deletion server.js
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down

0 comments on commit 18b5b5e

Please sign in to comment.