Reputation: 2975
I have the following piece of code as follows:
function(staticPath) {
console.log('static Path = ' +staticPath);
return function(data, response) {
console.log('data = ' +data);
console.log('response = ' +response);
var readStream;
// Fix so routes to /home and /home.html both work.
data = data.replace(/^(\/home)(.html)?$/i, '$1.html');
data = '.' + staticPath + data;
fs.stat(data, function(error, stats) {
if (error || stats.isDirectory()) {
return exports.send404(response);
}
readStream = fs.createReadStream(data);
return readStream.pipe(response);
});
}
}
This function basically parses the path to a HTML file and displays the contents of the file.
I am not able to understand how to call this method from outside.
I am calling it as follows:
staticFile("D:\\Node Applications\\public\\home.html")
I can capture it inside a variable innerFunc
and i can call the inner function as innerFunc(response)
where response is http's serverResponse of which i have the reference with me but i am not sure how can i pass the data
param.
I am not understanding what is happening behind the scenes. Can anyone explain ? Do we encounter such kind of code often in javascript ?
EDIT: To make things clear: There is another method as follows:
function(data, response) {
response.writeHead(200, {
'Content-Type': 'application/json'
});
response.end(JSON.stringify(data));
}
which i call from my node server logic as follows:
http.createServer(function(req, res) {
// A parsed url to work with in case there are parameters
var _url;
// In case the client uses lower case for methods.
req.method = req.method.toUpperCase();
console.log(req.method + ' ' + req.url);
if (req.method !== 'GET') {
res.writeHead(501, {
'Content-Type': 'text/plain'
});
return res.end(req.method + ' is not implemented by this server.');
}
if (_url is something like //localhost:1337/employees) {
//call employee service which returns *data*.
// send the data with a 200 status code
return responder.sendJson(data, res);
});
} else {
// try to send the static file
/*res.writeHead(200);
res.end('static file maybe');*/
console.log('Inside else');
var staticInner = responder.staticFile("D:\\Node Applications\\public\\home.html");
staticInner(res);
}
// res.end('The current time is ' + Date.now())
}).listen(1337, '127.0.0.1');
As one can see there is no data variable to pass to the innerFunc hence, got confused.
Upvotes: 0
Views: 121
Reputation: 6902
According to the "innerFunc", it seems like data
is some string that, being concatenated with staticPath
, forms a path to a file. As we can see, that path is tested with fs.stat
. Maybe the client is supposed to send some custom data? At any rate, data
seems to be a pretty bad name for a variable like that.
It seems like what that function is trying to do is to send a file as response? What staticPath
is supposed to be is a path relative to the Javascript file where this code is, because it is concatenated like this:
data = '.' + staticPath + data;
That would end in something like ./some/path/index.html
. staticPath
's value would be /some/path
and data
's value would be index.html
. So if your JS file is in /home/foo/node/index.js
, the file that the "innerFunc" will try to find would be in /home/foo/node/some/path/index.html
.
If you pass "D:\\Node Applications\\public\\home.html"
to staticFile()
as you're doing, you would get something like .D:\\Node Applications\\public\\home.htmlindex.html
which is clearly an invalid file.
To answer what's the point of a function returning a function
In this case, as mgouault said, it makes no sense at all to do that. Maybe the programmer had something in mind but changed it while programming this and the function ended up like that (it sometimes happen).
Upvotes: 1
Reputation: 78
Your functions (ex: function(staticPath)
) should either have a name or be stored in a variable to be able to call them. So I will guess that you are storing all this code in the variable staticFile
.
Then when you call it with a parameter: staticFile("D:\\Node Applications\\public\\home.html")
it returns a function (return function(data, response) {...}
).
Now that you have this inner function you can call it with data
and response
:
var innerFunc = staticFile("D:\\Node Applications\\public\\home.html");
innerFunc(data, response);
Which will responds using your variable response
(I guess).
A function returning a function is frequent in javascript especially when using closures for certains purposes, but in this case it's not really useful.
Upvotes: 0