Skip to content

Instantly share code, notes, and snippets.

@agsdot
Forked from sferik/fortune_server.js
Created May 24, 2013 21:40
Show Gist options
  • Save agsdot/5646698 to your computer and use it in GitHub Desktop.
Save agsdot/5646698 to your computer and use it in GitHub Desktop.
var fortunes = [
"Have a nice day",
"You look really good today",
"It's your birthday!",
"Never play leapfrog with a unicorn"
];
var net = require('net');
var server = net.createServer(function(connection){
connection.write("Enter the number of fortunes you want: ");
connection.on('data', function(data){
var num_fortunes = parseInt(data, 10);
if(isNaN(num_fortunes)) {
connection.write("That's not a number, silly.")
connection.end();
} else if(num_fortunes > 4) {
connection.write("You're too greedy. Ask for fewer fortunes next time.");
connection.end();
} else {
for(var i = 0; i < num_fortunes; i += 1) {
connection.write(fortunes[i] + "\n");
}
connection.end();
}
connection.on('error', function(){});
})
});
server.listen(1234);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment