In your specific example using the start() function you listed, you are
right that you do not use the query and postData parameters. If your
application is finished at that point, then no, you do not need to pass
those parameters - just leave them out.
But the point of the handle[pathname](response, query, postData) syntax
though is that there will most likely be a lot of different routes that
need to be handled. When there are, it would be considerably less
desirable to be testing the parameter requirements of the function that
each handle[pathname] resolves too or lots of if statements based on
pathname. That code would be hard to maintain and just be needlessly
messy. Why do that? Who knows whether it costs more in performance to
make that evaluation rather than just pass all of the same parameters to
all of the routes no matter if the route actually uses them or not?
Nodejs is very fast. Do you need to try to save a few cycles here? Is
what you propose faster than the alternative? Does it really matter in
the end for your particular application? Are there other more obvious
ways to enhance performance if you still need it?
Those are some of the things that I would think about before worrying
about whether the function that is routed to needs all of the parameters
passed or not. I do not know if it is inefficient to pass extra
parameters (I would guess not). You would probably have to test both
ways to find out though and then weigh the cost in light of your
particular requirements, if in fact, the performance requirement is not
being already met.
Kurt
On 3/31/12 6:04 PM, STT wrote:
Hello,
I've just started finding my way around node.js and to do that I have
used this http://www.nodebeginner.org/ tutorial. The author spreads
the different modules - server, router, etc. to different files, and
then uses require to hook them in in the main file.
Now, since they are all hooked in at the begining of the way, I'm
forced to pass arguments into functions, which these particular
functions don't actually need. For example:
route(handle, pathname, query, response, postData);
I pass to the router a list of handles, a pathname, the query attached
to it, the response object and any post data that I have received.
Then the router does this:
handle[pathname](response, query, postData);
Which finally links to this function:
function start(response, query, postData) {
console.log("RH start");
view.readFile("views/page.html","utf8",function (err,data){
if(err){
console.log(err);
return;
}
response.writeHead(200, {"Content-Type:": "text/html"});
response.write(data);
response.end();
});
}
It's just one of the few handlers that I have, but basically what
start actually needed was only the response object. Is this really the
way things are done? Because it seems a bit inefficient to me adding
parameters that aren't actually needed.
--
Kurt Symanzik
[email protected]
Skype id: ksymanzik
http://kbsymanzik.org
--
Job Board: http://jobs.nodejs.org/
Posting guidelines:
https://github.com/joyent/node/wiki/Mailing-List-Posting-Guidelines
You received this message because you are subscribed to the Google
Groups "nodejs" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/nodejs?hl=en?hl=en