简体   繁体   中英

how does this callback function work

I am new to nodejs and almost n00b of JavaScript. I saw the code to create a server using nodejs. I can understand that the anonymous function is called after a request reached the server.

var http=require("http");
http.createServer(function(request,response){
 response.writeHead(200,{"Content-Type":"text/plain"});
 response.write("hello world");
 responde.end();
}).listen(8888); 

My question is how to implement something similar like createServer function (foo() bar())..in order to understand how this method works.

In order to make it clear. I have done this that is not working. and how to make it work like createServer() ?

function dummycallback(para1,para2,callback)
{
 console.log('para1 is ' + para1+' para2 is '+ para2);
 callback();
}

dummycallback(1,2,function(req,res)
{
 req.senddata("good");
});

I have seen these code everywhere in nodejs so I am desperately want to know the details...thanks again

Maybe, you are asking how the anonymous function works.

var fun = function(foo){
    if (foo) foo(1, 2); ///< if function foo exists, call it.
}
fun(function(p1, p2){
    return p1 + p2;
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM