I am a beginner in node.js (infact started just today). One of the basic concepts is not clear to me, which I am asking here & couldn't find on SO.
Reading some tutorials on the web I wrote a client side & a server side code:
Server side (say server.js):
var http = require('http'); //require the 'http' module
//create a server
http.createServer(function (request, response) {
//function called when request is received
response.writeHead(200, {'Content-Type': 'text/plain'});
//send this response
response.end('Hello World\nMy first node.js app\n\n -Gopi Ramena');
}).listen(1337, '127.0.0.1');
console.log('Server running at http://127.0.0.1:1337/');
Client side (say client.js):
var http=require('http');
//make the request object
var request=http.request({
'host': 'localhost',
'port': 80,
'path': '/',
'method': 'GET'
});
//assign callbacks
request.on('response', function(response) {
console.log('Response status code:'+response.statusCode);
response.on('data', function(data) {
console.log('Body: '+data);
});
});
Now, to run the server, I type node server.js
in the terminal or cmd prompt. & it runs successfully logs the message in the console & also outputs the response when I browse to 127.0.0.1:1337.
But, how to I run client.js? I could not understand how to run the client side code.