Skip to content

Instantly share code, notes, and snippets.

@psema4
Created December 14, 2010 17:56
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save psema4/740789 to your computer and use it in GitHub Desktop.
Save psema4/740789 to your computer and use it in GitHub Desktop.
Simple Node.JS web server & client
// Modified from Node.JS documentation: http://nodejs.org/docs/v0.2.5/api.html#http-client-183
var http = require('http');
var server = http.createClient(8124, '127.0.0.1');
var request = server.request('GET', '/', {'host': '127.0.0.1'});
request.end();
request.on('response', function (response) {
console.log('STATUS: ' + response.statusCode);
console.log('HEADERS: ' + JSON.stringify(response.headers));
response.setEncoding('utf8');
response.on('data', function (chunk) {
console.log('BODY: ' + chunk);
});
});
// From Node.JS documentation: http://nodejs.org/
var http = require('http');
http.createServer(function (req, res) {
res.writeHead(200, {'Content-Type': 'text/plain'});
res.end('Hello World\n');
}).listen(8124, "127.0.0.1");
console.log('Server running at http://127.0.0.1:8124/');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment