Quantcast
Channel: Why we pass "app" in http.createServer(app) - Stack Overflow
Viewing all articles
Browse latest Browse all 3

Answer by jfriend00 for Why we pass "app" in http.createServer(app)

$
0
0

In your first example, I'm assuming that app represents an Express instance from something like this:

const app = express();

If so, then app is a request handler function that also has properties. You can pass it like this:

var server = http.createServer(app); 

because that app function is specifically designed to be an http request listener which is passed the arguments (req, res) from an incoming http request as you can see here in the doc.

Or, in Express, you can also do:

const server = app.listen(80);

In that case, it will do the http.createServer(app) for you and then also call server.listen(port) and return the new server instance.


When you do this:

https.createServer(function (req, res) {  res.writeHead(200, {'Content-Type': 'text/plain'});  res.write('Hello World!');  res.end();}).listen(port);

you are just making your own function that's built to handle an incoming http request instead of using the one that the Express library makes for you.


Viewing all articles
Browse latest Browse all 3

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>