What's the difference between creating a server using http module and creating a server using express framework in node js? Thanks.
-
1Really what you're asking is why would one use the Express framework instead of just using the http module. Basically, you can read about the features of the Express framework and decide if any of those are useful to you. It's a superset of the http module.jfriend00– jfriend002016-02-03 03:53:02 +00:00Commented Feb 3, 2016 at 3:53
-
I am actually novice into node.Koushik Das– Koushik Das2016-02-08 14:47:42 +00:00Commented Feb 8, 2016 at 14:47
-
1Possible duplicate of Express.js - app.listen vs server.listenShaishav Jogani– Shaishav Jogani2017-10-31 23:11:49 +00:00Commented Oct 31, 2017 at 23:11
-
25Very rude answers conssidering the public interest of the questionMr-Programs– Mr-Programs2018-11-19 05:10:39 +00:00Commented Nov 19, 2018 at 5:10
2 Answers
Ultimately, express uses node's http api behind the scenes.
express framework
The express framework provides an abstraction layer above the vanilla http module to make handling web traffic and APIs a little easier. There's also tons of middleware available for express (and express-like) frameworks to complete common tasks such as: CORS, XSRF, POST parsing, cookies etc.
http api
The http api is very simple and is used to to setup and manage incoming/outgoing ,HTTP connections. Node does most of the heavy lifting here but it does provide things you'll commonly see throughout most node web framework such as: request/response objects etc.
Comments
Express uses the http module under the hood, app.listen() returns an instance of http. You would use https.createServer if you needed to serve your app using HTTPS, as app.listen only uses the http module.
Here's the source for app.listen so you can see the similarities.:
app.listen = function(){
var server = http.createServer(this);
return server.listen.apply(server, arguments);
};