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
2 Answers
Reset to default 6http.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!