Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why we pass "app" in http.createServer(app)

Why we pass "app" in http.createServer(app) as we can also pass

e.g :

var app = require('./app')
const http = require('http')
const port = 3500 || process.env.PORT


var server = http.createServer(app) //here we pass app

in other code we pass some different argument such as this

https.createServer(function (req, res) {
  res.writeHead(200, {'Content-Type': 'text/plain'});
  res.write('Hello World!');
  res.end();
}).listen(port)
like image 270
Amaan Imtiyaz Avatar asked Mar 19 '20 21:03

Amaan Imtiyaz


People also ask

Why do we use app get?

app.get() method The method is used to expose the GET method. It is intended for binding middleware to your application. The path is a mount path and limits the middleware to only apply any paths requested that begin with it. It is intended for matching and handling a specific route when requested by get http.

What is app use in Express JS?

The app. use() method mounts or puts the specified middleware functions at the specified path. This middleware function will be executed only when the base of the requested path matches the defined path.

What is the difference between APP get and app post?

GET and POST is two common HTTP Requests used for building REST APIs. Both of these calls are meant for some special purpose. As per the documentation GET requests are meant to fetch data from specified resources and POST requests are meant to submit data to a specified resource.

Why should you separate express APP and server?

Faster testing execution. Getting wider coverage metrics of the code. Allows deploying the same API under flexible and different network conditions. Better separation of concerns and cleaner code.


2 Answers

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.

like image 154
jfriend00 Avatar answered Oct 02 '22 15:10

jfriend00


Quoting the Express documentation: The app returned by express() is in fact a JavaScript Function, designed to be passed to Node’s HTTP servers as a callback to handle requests. This makes it easy to provide both HTTP and HTTPS versions of your app with the same code base, as the app does not inherit from these (it is simply a callback):

    var express = require('express')
    var https = require('https')
    var http = require('http')
    var app = express()

    http.createServer(app).listen(80)
    https.createServer(options, app).listen(443)

https://expressjs.com/en/api.html

like image 33
john-raymon Avatar answered Oct 02 '22 15:10

john-raymon