最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How does http.createServer() know the parameters of the callback? - Stack Overflow

programmeradmin2浏览0评论

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

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

Can someone show me an example of how to create a function like createServer

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

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

Can someone show me an example of how to create a function like createServer

Share Improve this question edited May 23, 2024 at 2:46 Icarus 1,6577 gold badges20 silver badges34 bronze badges asked Jan 1, 2021 at 3:36 Moe Moe 31 silver badge6 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 6

http.createServer() has no idea what you declared for callback parameters. Regardless of how you declare your callback, http.createServer() passes two arguments to the callback when you call it. The first is the http request object, the second is the http response object. If your callback wants to work properly and use those arguments, it must create two arguments that match those. 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

You can create a function that accepts a callback function and then you call that callback with a set of 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. 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/api/http.html, and when something like a request happens, the server returns your callback function with some parameters.

Hopefully, it will be easier to understand with 1 example:

You have the following 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:

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 pletely misunderstood your question, feel free to tell me!

发布评论

评论列表(0)

  1. 暂无评论