Created
November 9, 2010 15:31
-
-
Save FrankGrimm/669233 to your computer and use it in GitHub Desktop.
HTTP body in node.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var http = require('http'); | |
http.createServer(function (req, res) { | |
// set up some routes | |
switch(req.url) { | |
case '/': | |
console.log("[501] " + req.method + " to " + req.url); | |
res.writeHead(501, "Not implemented", {'Content-Type': 'text/html'}); | |
res.end('<html><head><title>501 - Not implemented</title></head><body><h1>Not implemented!</h1></body></html>'); | |
break; | |
case '/formhandler': | |
console.log("[501] " + req.method + " to " + req.url); | |
res.writeHead(501, "Not implemented", {'Content-Type': 'text/html'}); | |
res.end('<html><head><title>501 - Not implemented</title></head><body><h1>Not implemented!</h1></body></html>'); | |
break; | |
default: | |
res.writeHead(404, "Not found", {'Content-Type': 'text/html'}); | |
res.end('<html><head><title>404 - Not found</title></head><body><h1>Not found.</h1></body></html>'); | |
console.log("[404] " + req.method + " to " + req.url); | |
}; | |
}).listen(8080); // listen on tcp port 8080 (all interfaces) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
case '/': | |
// show the user a simple form | |
console.log("[200] " + req.method + " to " + req.url); | |
res.writeHead(200, "OK", {'Content-Type': 'text/html'}); | |
res.write('<html><head><title>Hello Noder!</title></head><body>'); | |
res.write('<h1>Welcome Noder, who are you?</h1>'); | |
res.write('<form enctype="application/x-www-form-urlencoded" action="/formhandler" method="post">'); | |
res.write('Name: <input type="text" name="username" value="John Doe" /><br />'); | |
res.write('Age: <input type="text" name="userage" value="99" /><br />'); | |
res.write('<input type="submit" />'); | |
res.write('</form></body></html'); | |
res.end(); | |
break; | |
case '/formhandler': | |
console.log("[501] " + req.method + " to " + req.url); | |
res.writeHead(501, "Not implemented", {'Content-Type': 'text/html'}); | |
res.end('<html><head><title>501 - Not implemented</title></head><body><h1>Not implemented!</h1></body></html>'); | |
break; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
case '/formhandler': | |
if (req.method == 'POST') { | |
console.log("[200] " + req.method + " to " + req.url); | |
req.on('data', function(chunk) { | |
console.log("Received body data:"); | |
console.log(chunk.toString()); | |
}); | |
req.on('end', function() { | |
// empty 200 OK response for now | |
res.writeHead(200, "OK", {'Content-Type': 'text/html'}); | |
res.end(); | |
}); | |
} else { | |
console.log("[405] " + req.method + " to " + req.url); | |
res.writeHead(405, "Method not supported", {'Content-Type': 'text/html'}); | |
res.end('<html><head><title>405 - Method not supported</title></head><body><h1>Method not supported.</h1></body></html>'); | |
} | |
break; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var http = require('http'); | |
/* Add the following require() calls at the top | |
var querystring = require('querystring'); | |
var utils = require('utils'); | |
*/ | |
case '/formhandler': | |
if (req.method == 'POST') { | |
console.log("[200] " + req.method + " to " + req.url); | |
var fullBody = ''; | |
req.on('data', function(chunk) { | |
// append the current chunk of data to the fullBody variable | |
fullBody += chunk.toString(); | |
}); | |
req.on('end', function() { | |
// request ended -> do something with the data | |
res.writeHead(200, "OK", {'Content-Type': 'text/html'}); | |
// parse the received body data | |
var decodedBody = querystring.parse(fullBody); | |
// output the decoded data to the HTTP response | |
res.write('<html><head><title>Post data</title></head><body><pre>'); | |
res.write(utils.inspect(decodedBody)); | |
res.write('</pre></body></html>'); | |
res.end(); | |
}); | |
} else { | |
console.log("[405] " + req.method + " to " + req.url); | |
res.writeHead(405, "Method not supported", {'Content-Type': 'text/html'}); | |
res.end('<html><head><title>405 - Method not supported</title></head><body><h1>Method not supported.</h1></body></html>'); | |
} | |
break; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var http = require('http'); | |
http.createServer(function (req, res) { | |
// set up some routes | |
switch(req.url) { | |
case '/': | |
// show the user a simple form | |
console.log("[200] " + req.method + " to " + req.url); | |
res.writeHead(200, "OK", {'Content-Type': 'text/html'}); | |
res.write('<html><head><title>Hello Noder!</title></head><body>'); | |
res.write('<h1>Welcome Noder, who are you?</h1>'); | |
res.write('<form enctype="application/x-www-form-urlencoded" action="/formhandler" method="post">'); | |
res.write('Name: <input type="text" name="username" value="John Doe" /><br />'); | |
res.write('Age: <input type="text" name="userage" value="99" /><br />'); | |
res.write('<input type="submit" />'); | |
res.write('</form></body></html'); | |
res.end(); | |
break; | |
case '/formhandler': | |
if (req.method == 'POST') { | |
console.log("[200] " + req.method + " to " + req.url); | |
var fullBody = ''; | |
req.on('data', function(chunk) { | |
// append the current chunk of data to the fullBody variable | |
fullBody += chunk.toString(); | |
}); | |
req.on('end', function() { | |
// request ended -> do something with the data | |
res.writeHead(200, "OK", {'Content-Type': 'text/html'}); | |
// parse the received body data | |
var decodedBody = querystring.parse(fullBody); | |
// output the decoded data to the HTTP response | |
res.write('<html><head><title>Post data</title></head><body><pre>'); | |
res.write(utils.inspect(decodedBody)); | |
res.write('</pre></body></html>'); | |
res.end(); | |
}); | |
} else { | |
console.log("[405] " + req.method + " to " + req.url); | |
res.writeHead(405, "Method not supported", {'Content-Type': 'text/html'}); | |
res.end('<html><head><title>405 - Method not supported</title></head><body><h1>Method not supported.</h1></body></html>'); | |
} | |
break; | |
default: | |
res.writeHead(404, "Not found", {'Content-Type': 'text/html'}); | |
res.end('<html><head><title>404 - Not found</title></head><body><h1>Not found.</h1></body></html>'); | |
console.log("[404] " + req.method + " to " + req.url); | |
}; | |
}).listen(8080); // listen on tcp port 8080 (all interfaces) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I read your blog on the node.js POST data. Recently I came across a problem where I even though using the body-parser middleware, I am unable to read the POST data sent to my webhook (built on node.js). I would appreciate if you can take a look at it and let me know what I am doing wrong. I opened an issue on the body-parser module git : https://github.com/expressjs/body-parser/issues/213