Skip to content

Instantly share code, notes, and snippets.

@ksato9700
Created December 17, 2011 08:00
Show Gist options
  • Save ksato9700/1489617 to your computer and use it in GitHub Desktop.
Save ksato9700/1489617 to your computer and use it in GitHub Desktop.
HTTP client and server by node.js
var net = require('net')
console.log("main")
var client = net.connect(8080, function() {
console.log("connected")
var req_message = "GET / HTTP/1.0\r\n\r\n"
client.write(req_message)
});
client.on('data', function(data) {
console.log("read");
console.log(data.toString());
// client.end();
});
client.on('end', function() {
console.log("closed");
});
client.on('error', function(exception) {
console.log(exception);
});
var http = require('http');
http.createServer(function (req, res) {
res.writeHead(200, {'Content-Type': 'text/plain'});
res.write('Hello World\n');
setTimeout(function() {
res.end('Hello Again\n');
}, 1000);
}).listen(8080, "127.0.0.1");
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment