You can see "(...)=>" symbol in the very first line of this code:
const server = http.createServer((req,res) => {
res.statusCode = 200;
res.setHeader('content-type', 'text/plain');
res.end('Hello World');
});
It's an arrow function, newly defined in ES6.
An arrow function expression has a shorter syntax than a function expression and does not bind its own this, arguments, super, or new.target. Arrow functions are always anonymous. These function expressions are best suited for non-method functions, and they cannot be used as constructors.
They are often just a shorter way of writing the anonymous function function () {}
, with which you may already be familiar.
These pieces of code do the same thing:
setTimeout(function () {
console.log("Hey");
}, 1000);
setTimeout(() => {
console.log("Hey");
}, 1000);
This means that in your example http.createServer
is accepting one argument, a function which itself takes two arguments.
Arrow functions are not equivalent to function () {}
anonymous functions, function () {}
binds its own this
, for example.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With