I'm reading example express source code for my education.
Initializing a simple express application looks like this:
const express = require('express')
const app = express()
app.listen(3000, () => {
console.log(`http://localhost:3000`)
})
I want to understand what app.listen
does in above code.
The source code of app.listen
is defined in express/lib/application.js, as following:
var app = exports = module.exports = {};
// ...
app.listen = function listen() {
var server = http.createServer(this); // <-- why `this` ??
return server.listen.apply(server, arguments);
};
Typically, http.createServer()
accepts a function as request handler. In this case, this
was passed in. But this
should refer to app
, it's not a function, right?
Later I found out app.handle was used as the request handler.
What is happening under the hood?
Thanks for your time!