有什么区别:
http.Server(function(req,res) {});
和
http.createServer(function(req, res) {});
最佳答案
基于nodejs的源码(摘录如下),createServer
只是实例化Server
的辅助方法。
摘自 line 1674 of http.js .
exports.Server = Server;
exports.createServer = function(requestListener) {
return new Server(requestListener);
};
因此,您在原始问题中提到的两个代码片段的唯一true 区别在于您没有使用 new
关键字。
为了清楚起见,Server
构造函数如下(2012-12-13 后):
function Server(requestListener) {
if (!(this instanceof Server)) return new Server(requestListener);
net.Server.call(this, { allowHalfOpen: true });
if (requestListener) {
this.addListener('request', requestListener);
}
// Similar option to this. Too lazy to write my own docs.
// http://www.squid-cache.org/Doc/config/half_closed_clients/
// http://wiki.squid-cache.org/SquidFaq/InnerWorkings#What_is_a_half-closed_filedescriptor.3F
this.httpAllowHalfOpen = false;
this.addListener('connection', connectionListener);
this.addListener('clientError', function(err, conn) {
conn.destroy(err);
});
}
util.inherits(Server, net.Server);
关于javascript - Node.js 的 http.Server 和 http.createServer,有什么区别?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13857747/