Skip to content

Instantly share code, notes, and snippets.

@Pablosan
Created January 28, 2011 02:38
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 Pablosan/799730 to your computer and use it in GitHub Desktop.
Save Pablosan/799730 to your computer and use it in GitHub Desktop.
A collection of simple services using nodeJS
var sys = require('sys');
setTimeout(function () {
sys.puts('nodeJS!');
}, 2000);
sys.puts('Hello');
var puts = require('sys').puts;
setInterval(function () {
puts('Hello!');
}, 500);
process.addListener('SIGINT', function () {
puts('\nGoodbye!');
process.exit(0)
});
var net = require('net');
var server = net.createServer(function(socket) {
socket.write('Hello... and Goodbye!\n');
socket.end();
});
server.listen(8000);
var stat = require('fs').stat,
puts = require('sys').puts;
var promise = stat('/etc/passwd', function(error, stats) {
if (error) throw error;
puts('\n/etc/passwd last modified ' + stats.mtime + '\n');
});
var http = require('http');
http.createServer(function (request, response) {
response.writeHead(200, {'Content-Type': 'text/plain'});
response.end('Hello World!\n');
}).listen(8124, "127.0.0.1");
var http = require('http');
http.createServer(function (request, response) {
response.writeHead(200, {'Content-Type': 'text/plain'});
response.write('Hello\n');
setTimeout(function() {
response.write('World!\n');
response.end();
}, 2000);
}).listen(8124, "127.0.0.1");
var puts = require('sys').puts;
var spawn = require('child_process').spawn;
var cat = spawn('cat');
cat.stdout.on('data', function(data) {
if (data) puts(data);
});
cat.stdin.write('Hello ');
setTimeout(function() {
cat.stdin.write('nodeJS!');
cat.stdin.end();
}, 2000);
@Pablosan
Copy link
Author

Uses nodeJS version v0.3.6-pre

••••••••
01.node_sys.js demonstrates non-blocking IO:
••••••••

On the command line:
node 01.node_sys.js

Responds with:
Hello
<2 seconds later>
nodeJS!

••••••••
02.node_sigint.js demonstrates event-based model
••••••••

On the command line:
node 02.node_sigint.js

Responds with:
"Hello!\n" every 500 ms. until
user enters +C, then prints
"\nGoodbye!" and exits.

••••••••
03.node_tcp.js demonstrates a TCP server
••••••••

On the command line:
node 03.node_tcp.js

Then:
telnet 127.0.0.1 8000

Responds With:
Trying 127.0.0.1...
Connected to localhost.
Escape character is '^]'.
Hello... and Goodbye!
Connection closed by foreign host.

••••••••
04.node_file_io.js demonstrates File IO
••••••••

On the command line:
node 04.node_file_io.js

Responds With Something Similar To:
/etc/passwd last modified Fri Sep 24 2010 23:26:18 GMT-0500 (CDT)
(assuming you're on a system with a /etc/passwd file)

••••••••
05.node_http_hello_world.js demonstrates an HTTP server
••••••••

On the command line:
node 05.node_http_hello_world.js

Then:
curl -i http://127.0.0.1:8124

Responds With:
HTTP/1.1 200 OK
Content-Type: text/plain
Connection: keep-alive
Transfer-Encoding: chunked

Hello World!

••••••••
06.node_http_long_poll.js demonstrates a long-poll HTTP server
••••••••

On the command line:
node 06.node_http_long_poll.js

Then:
curl -i http://127.0.0.1:8124

Responds With:
HTTP/1.1 200 OK
Content-Type: text/plain
Connection: keep-alive
Transfer-Encoding: chunked

Hello
<2 second delay, then...>
World!

••••••••
07.node_streaming_io.js demonstrates streaming IO
••••••••

On the command line:
node 07.node_streaming_io.js

Responds With:
Hello
<2 second delay, then...>
nodeJS!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment