Created
January 28, 2011 02:38
-
-
Save Pablosan/799730 to your computer and use it in GitHub Desktop.
A collection of simple services using nodeJS
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var sys = require('sys'); | |
setTimeout(function () { | |
sys.puts('nodeJS!'); | |
}, 2000); | |
sys.puts('Hello'); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var puts = require('sys').puts; | |
setInterval(function () { | |
puts('Hello!'); | |
}, 500); | |
process.addListener('SIGINT', function () { | |
puts('\nGoodbye!'); | |
process.exit(0) | |
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var net = require('net'); | |
var server = net.createServer(function(socket) { | |
socket.write('Hello... and Goodbye!\n'); | |
socket.end(); | |
}); | |
server.listen(8000); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'); | |
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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
••••••••
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
••••••••
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!