Skip to content

Instantly share code, notes, and snippets.

@pgte
Created July 6, 2010 08:55
Show Gist options
  • Save pgte/465185 to your computer and use it in GitHub Desktop.
Save pgte/465185 to your computer and use it in GitHub Desktop.
var sys = require( 'sys' );
var http = require('http');
var form_doc = '\
<html> \
<body>\
<form method="POST" action="/body">\
<input type="submit" name="test" value="Body" />\
</form>\
<form method="POST" action="/nobody">\
<input type="submit" name="test" value="No body" />\
</form>\
</body>\
</html'
http.createServer(function (request, response) {
if (request.method == 'GET') {
response.writeHead(200, {'Content-Type': 'text/html'});
response.write(form_doc, 'utf-8');
response.end();
} else if (request.method == 'POST') {
if (request.url == '/body') {
request.addListener('data', function(chunk) {
sys.puts("--> "+chunk);
});
request.addListener('end', function() {
//sys.puts("request ended");
response.writeHead(200, {'Content-Type': 'text/html'});
response.write('body');
response.end();
});
} else if (request.url == '/nobody') {
response.writeHead(200, {'Content-Type': 'text/html'});
response.write('no body checked');
}
}
}).listen(3000);
sys.puts('Server running at http://localhost:3000/');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment