Difference between a server with http.createServer and a server using express in node js

╄→гoц情女王★ 提交于 2019-12-30 03:47:11

问题


What's the difference between creating a server using http module and creating a server using express framework in node js? Thanks.


回答1:


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.




回答2:


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);
};


来源:https://stackoverflow.com/questions/35167824/difference-between-a-server-with-http-createserver-and-a-server-using-express-in

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!