Skip to content

Instantly share code, notes, and snippets.

@raghav4
Forked from FrankGrimm/nodepost-1.js
Created March 11, 2020 13:04
Show Gist options
  • Save raghav4/4e3664682392442d233db27f404d8e28 to your computer and use it in GitHub Desktop.
Save raghav4/4e3664682392442d233db27f404d8e28 to your computer and use it in GitHub Desktop.
HTTP body in node.js
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)
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;
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;
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;
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