var express = require("express")
, app = express()
, http = require("http").createServer(app)
我经常看到这些被放在依赖项上。从我的理解来看,http托管前端html?并且 express 拥有服务器端的 nodejs 逻辑? connect 是 express 的基础层,那么它也是一个服务器端模块吗?
如果不是这样,为什么人们不这样做
express().listen(8080)
代替
require("http").createServer(express()).listen(8080)
最佳答案
Express
是 http
之上的另一层。它负责 Web 应用程序所需的基本重复任务。 connect
是一个中间件,它也负责 Web 应用程序所需的基本重复任务。
使用任何框架背后的整体理念是保持DRY,不要重复自己。 解析请求正文、解析 cookie 等任务对于每个 Web 应用程序来说都是非常需要的。 Express
默认 提供它们。
在Express 4.x中,大部分已经被重构为几个细粒度的模块,如body-parser
、morgan
等,可以直接使用,不需要使用 express 。
请引用Express - GitHub完整的模块集。
Express
是 http
之上的另一层,意味着 Express
内部使用 http
。换句话说,它是 http
的包装器。如果你查看Express
的源代码,你会发现它内部使用了http
。请参阅突出显示的语句
https://github.com/strongloop/express/blob/master/lib/application.js#L540-543
关于javascript - express 和 http 之间的技术区别是什么,并就此进行连接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25151726/