Node.js: Difference between revisions
Jump to navigation
Jump to search
No edit summary |
|||
Line 6: | Line 6: | ||
= http server = | = http server = | ||
http://www.youtube.com/watch?v=jo_B4LTHi3I | |||
First create a js file | First create a js file | ||
<pre> | <pre> | ||
Line 29: | Line 31: | ||
OR use "-i" to include HTTP-header in the output | OR use "-i" to include HTTP-header in the output | ||
curl -i http://localhost:8000 | curl -i http://localhost:8000 | ||
</pre> | |||
= tcp server = | |||
Create <tcp-server.js> file | |||
<pre> | |||
var net= require('net') | |||
var server = net.createServer(function(socket) { | |||
socket.write('hello\n'); | |||
socket.end('world\n'); | |||
}); | |||
server.listen(8000); | |||
</pre> | |||
After running this program, we can test it by using '''telnet''' or '''nc'''. | |||
<pre> | |||
telnet localhost 8000 | |||
OR | |||
nc localhost 8000 | |||
</pre> | </pre> |
Revision as of 13:21, 11 October 2013
node.js prompt
How to exit
.exit
http server
http://www.youtube.com/watch?v=jo_B4LTHi3I
First create a js file
var http = require('http'); var s = http.createServer(function (req, res) { res.writeHead(200, {'Content-Type': 'text/plain'}); res.write('Hello \n'); setTimeout(function() { res.end("world\n"); }, 2000); }); s.listen(8000);
Then using node program to run the js script. We can test the program by using a web browser or curl program.
# Terminal 1 node web-server.js # Terminal 2 curl http://localhost:8000 OR use "-i" to include HTTP-header in the output curl -i http://localhost:8000
tcp server
Create <tcp-server.js> file
var net= require('net') var server = net.createServer(function(socket) { socket.write('hello\n'); socket.end('world\n'); }); server.listen(8000);
After running this program, we can test it by using telnet or nc.
telnet localhost 8000 OR nc localhost 8000