简体   繁体   English

http.createServer()怎么知道回调的参数?

[英]How does http.createServer() know the parameters of the callback?

here is the picture这是图片

http.createServer((req,res)=>{}); http.createServer((req,res)=>{});

how the createServer function knows that the callback function has 'req' for incoming message object and 'res' for server response object automatically how the createServer function knows that the callback function has 'req' for incoming message object and 'res' for server response object automatically

Can someone show me an example of how to create a function like createServer有人可以告诉我一个如何创建 function 之类的示例createServer

http.createServer() has no idea what you declared for callback parameters. http.createServer()不知道您为回调参数声明了什么。 Regardless of how you declare your callback, http.createServer() passes two arguments to the callback when you call it.无论您如何声明您的回调, http.createServer()在您调用它时将两个 arguments 传递给回调。 The first is the http request object, the second is the http response object.第一个是 http 请求 object,第二个是 http 响应 ZA8CFDE6331BD59EB26666F8911CB4。 If your callback wants to work properly and use those arguments, it must create two arguments that match those.如果您的回调想要正常工作并使用那些 arguments,它必须创建两个与之匹配的 arguments。 You can name them anything you want.您可以随意命名它们。 The name is local only to your callback.该名称仅对您的回调是本地的。

So, you can do any of these:因此,您可以执行以下任何操作:

http.createServer((request, response) => {
    response.end("hi");
});

http.createServer((req, res, goop) => {
    // goop will be undefined here since no third argument is passed to the callback
    res.end("hi");
});

http.createServer((a, b) => {
    b.end("hi");
});

http.createServer((...args) => {
    args[1].end("hi");
});

http.createServer(function() {
    arguments[1].end("hi");
});

Can someone show me an example of how to create a function like createServer有人可以告诉我一个如何创建 function 之类的示例 createServer

You can create a function that accepts a callback function and then you call that callback with a set of arguments:您可以创建一个接受回调 function 的 function,然后使用一组 arguments 调用该回调:

function readJSON(filename, callback) {
    fs.readFile(filename, function(err, data) {
        if (err) {
            callback(err);
        } else {
            try {
                let json = JSON.parse(data);
                callback(null, json);
            } catch(e) {
                callback(e);
            }
        }
    });
}              
            

Here you create a function that takes a filename and a callback.在这里,您创建了一个 function,它接受一个文件名和一个回调。 It then reads that file, parses the JSON in it and calls the callback with null for the err and the parsed Javascript object for the 2nd parameter. It then reads that file, parses the JSON in it and calls the callback with null for the err and the parsed Javascript object for the 2nd parameter. If there's an error, then it just passes the error as the first argument.如果有错误,那么它只是将错误作为第一个参数传递。

HTTP is a class that contains 1 function called createServer, you can find some details on the HTTP object here https://nodejs.org/api/http.html , and when something like a request happens, the server returns your callback function with some parameters. HTTP is a class that contains 1 function called createServer, you can find some details on the HTTP object here https://nodejs.org/api/http.html , and when something like a request happens, the server returns your callback function with一些参数。

Hopefully, it will be easier to understand with 1 example:希望通过 1 个示例更容易理解:

You have the following class:您有以下 class:

class User {
  userInfo = {
    name: "Jhon",
    surname: "Doe"
  }
  getFullName = (callback) => {
    return callback(this.userInfo.name, this.userInfo.surname)
  }
}

This class has 2 defined properties, userInfo that contains the name and surname of the "user", and 1 function, getFullName, that function expects another function as is input, and whenever we call it, we will receive 2 parameters as output: This class has 2 defined properties, userInfo that contains the name and surname of the "user", and 1 function, getFullName, that function expects another function as is input, and whenever we call it, we will receive 2 parameters as output:

const UserInfo = new User()
UserInfo.getFullName((name,surname) => console.log(`Hello ${name} ${surname}`))

Hopefully, that answers your question, if you have any further questions or I completely misunderstood your question, feel free to tell me!希望这能回答您的问题,如果您还有其他问题或我完全误解了您的问题,请随时告诉我!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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