In express.js there is the app.listen convenience method that can be used to get an express app to start listening for requests on a given port. In many projects this express.js app object method will work just find, but in some situations you might want to use the native node.js http or https methods to get your express app up and running. In this post in will be giving some quick examples of app.listen in express.js as well as the http.createServer node.js http module method as well.
Any Express.js app will involve creating an instance of an app object my calling the main method that is obtained when requiring express in node.js. This app object can then be used to define what is to be done when certain requests are made from a client, and so forth. However in order to get this app up and running and start listening on a port for requests the app.listen method or something else to that effect must be used to start responding to traffic.
One simple way to use the app listen method is to just call it off the instance of the app object and pass an optional port number, and a callback that will fire once the app has started listening.
Here I am of course just giving a hard coded port number of 8080, but when creating a more serious project that will be deployed the situation will often be different. Often a port number will be passed as an argument, or will be available via an environment variable.
If for some reason I need to use the native node.js createServer method in the http or https modules doing so is very easy. I can just pass the app object as the first argument to createServer and then call the listen method of what is returned by create server is more or less the same way as the app listen method in express.
I will not be getting into how to go about doing just that in detail in this post, if interested I have written a post on using just the http module by itself before. However I will quickly go over just a simple example of using the native node.js method behind app listen here.