javascript - node.js - can someone explain this callback? -
there's more 1 node.js tutorial out there, demonstrating how create server. but, they're coded in different ways. so, how know when write 1 way, versus another? none of tutorials explain why wrote way did.
for example...
example 1:
// create http server, passing in function reason: http.createserver(function(request, response) {
versus example 2:
// create http server, , start var server = http.createserver(handlerequest); server.listen(port, function () {
thanks in advance. i'm having trouble understanding this.
they're no different, really. in first, you're defining function handling request on spot have start listening @ end.
http.createserver(function(request, response) { ... }).listen(port, function() { ... });
in second, define function handling request elsewhere , don't use chaining.
function handlerequest(request, response) { ... } var server = http.createserver(handlerequest); server.listen(port, function() { ... });
they both same thing. depends on how want write it.
Comments
Post a Comment