Vikas Kumar Vikas Kumar - 1 year ago 99
Node.js Question

What is client, path and data in this code?

I'm learning Node.js I have created server and client .js files but I don't understand few things. For example, in the webserver.js file, I don't know what is the use of

. Similarly, in the
file, what are

If you think I should read about the basics of it, please provide me a useful link if you can. I tried to find but didn't work.


var fs=require('fs');
var url=require('url');
var http=require('http');
http.createServer(function(request, response){
var pathname=url.parse(request.url).pathname;
console.log("Pathname: "+pathname+"Request.url: "+request.url);
fs.readFile(pathname.substr(1), function(err, data){
console.log("Error reading.");
response.writeHead(400, {'content-type' : 'text/html'});
response.writeHead(200, {'content-type' : 'text/html'});
console.log("Server is running.");


var http=require('http');
var options={
host: 'localhost',
port: '8081',
path: '/index.html'
var callback=function(response){
var body='';
response.on('data', function(data){
response.on('end', function(){
console.log("Data received.");
var req=http.request(options, callback);

The original code souce is here: Code

Answer Source

pathname is the path requested to the http server. The example pathname for this question is /questions/40276802/what-is-client-path-and-data-in-this-code. The path in client.js is the same deal.

You can find documentation on parsing the URL into the pathname from the Node.js docs:

Node's HTTP client uses streams, which emit several events. data is called with a buffer, which you usually will add to an array then concat later (as the code does). end is called when all buffers are sent.

You can find documentation on handling events from streams from the Node.js docs:

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download