Skip to content

Instantly share code, notes, and snippets.

@rizowski
Created May 28, 2014 14:45
Show Gist options
  • Save rizowski/15f522fc34b88370118c to your computer and use it in GitHub Desktop.
Save rizowski/15f522fc34b88370118c to your computer and use it in GitHub Desktop.
[TW] Node.js Examples (v0.10.28)
var http = require('http');
http.createServer(function(request, response) {
response.writeHead(200, {'content-Type' : 'text/plain'});
response.end("helloWorld");
}).listen(8080, '127.0.0.1');
console.log("Running Server on http://127.0.0.1:8080");
var net = require('net'),
readline = require('readline');
var client = new net.Socket();
client.connect(8081, '127.0.0.1', function() {
console.log('Connected');
var rl = readline.createInterface({
input: process.stdin,
output: process.stdout
});
rl.on('line', function(cmd){
console.log("Sending: "+ cmd);
client.write(cmd);
});
});
client.on('data', function(data) {
console.log('Received: ' + data);
});
client.on('close', function() {
console.log('Connection closed');
});
var net = require('net'),
readline = require('readline');
var server = net.createServer();
server.on('connection', function(socket){
console.log("Connected! " + socket.remoteAddress);
socket.write("Welcome " + socket.remoteAddress);
socket.on('data', function(message){
console.log('Recieved "' + message + '". Applying mistifications.');
message = message + "!";
socket.write(message);
});
});
server.listen(8081, '127.0.0.1');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment