Friday, April 6, 2012

Hello world in Node.js

First you need to install node.js. I'm running ubuntu 11.10 and I also needed to
$ apt-get install openssl libssl-dev

Create a file named test.js, and copy and paste the code below

//how to run the server output hello world
var http = require('http');

var server = http.createServer(function(request, response){
response.writeHead(200, {
'Content-type' : 'text/plain'
});
response.end('hello world');
});
// where the host in located
server.listen(8000); //see the code below
console.log('listening on http://127.0.0.1:8000'); //echo in the terminal whatever you want
To start the server go to the terminal and the run the script you created by typing
$ node test.js
Now go to the browser and enter http://127.0.0.1:8000 and your done.

Resource: http://net.tutsplus.com/tutorials/javascript-ajax/this-time-youll-learn-node-js/

No comments:

Post a Comment