0
0
mirror of https://github.com/nodejs/node.git synced 2024-11-24 20:29:23 +01:00
nodejs/http_api.js
Ryan 19478ed4b1 Major refactoring: program name now "node"
Trying to make a more moduler design. Two libraries currently "TCP" and
"HTTP" each have their own file.

Other major feature added here is multiple web servers! excitement.
2009-03-03 01:56:15 +01:00

31 lines
769 B
JavaScript

function encode(data) {
var chunk = data.toString();
return chunk.length.toString(16) + "\r\n" + chunk + "\r\n";
}
var server = new HTTP.Server("localhost", 8000);
server.onRequest = function (request) {
log( "path: " + request.path );
log( "query string: " + request.query_string );
// onBody sends null on the last chunk.
request.onBody = function (chunk) {
if(chunk) {
this.respond(encode(chunk));
} else {
this.respond(encode("\n"));
this.respond("0\r\n\r\n");
this.respond(null); // signals end-of-request
}
}
request.respond("HTTP/1.0 200 OK\r\n");
request.respond("Content-Type: text/plain\r\n");
request.respond("Transfer-Encoding: chunked\r\n");
request.respond("\r\n");
};
/*
server.close();
*/