node.js http.createServer(app) 诉 http.Server(app)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/26921117/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
http.createServer(app) v. http.Server(app)
提问by 7stud
On the socket.io web page, Get Started: Chat application, located here:
在 socket.io 网页上Get Started: Chat application,位于此处:
http://socket.io/get-started/chat/
http://socket.io/get-started/chat/
there is this code:
有这个代码:
var app = require('express')();
var http = require('http').Server(app);
which could be rewritten a little more clearly like this:
可以像这样更清楚地重写:
var express = require('express');
var http = require('http');
var app = express();
var server = http.Server(app);
The socket.io example uses http.Server() to create a server. Yet, the express docs for app.listen()show an example where the server is created using http.createServer(app):
socket.io 示例使用 http.Server() 创建服务器。然而,app.listen()的 express 文档显示了一个使用http.createServer(app)以下方法创建服务器的示例:
app.listen()
Bind and listen for connections on the given host and port. This method is identical to node's http.Server#listen().var express = require('express'); var app = express(); app.listen(3000);The app returned by express() is in fact a JavaScript Function, designed to be passed to node's HTTP servers as a callback to handle requests. This allows you to provide both HTTP and HTTPS versions of your app with the same codebase easily, as the app does not inherit from these (it is simply a callback):
var express = require('express'); var https = require('https'); var http = require('http'); var app = express(); http.createServer(app).listen(80); https.createServer(options, app).listen(443);The app.listen() method is a convenience method for the following (if you wish to use HTTPS or provide both, use the technique above):
app.listen = function(){ var server = http.createServer(this); return server.listen.apply(server, arguments); };
app.listen()
绑定并侦听给定主机和端口上的连接。此方法与节点的 http.Server#listen() 相同。var express = require('express'); var app = express(); app.listen(3000);express() 返回的应用程序实际上是一个 JavaScript 函数,旨在作为回调传递给节点的 HTTP 服务器来处理请求。这允许您轻松地提供具有相同代码库的应用程序的 HTTP 和 HTTPS 版本,因为应用程序不会从这些版本继承(它只是一个回调):
var express = require('express'); var https = require('https'); var http = require('http'); var app = express(); http.createServer(app).listen(80); https.createServer(options, app).listen(443);app.listen() 方法是用于以下操作的便捷方法(如果您希望使用 HTTPS 或同时提供两者,请使用上述技术):
app.listen = function(){ var server = http.createServer(this); return server.listen.apply(server, arguments); };
What's the difference between http.createServer(app)and http.Server(app)?? The http docs are no help.
什么之间的差异http.createServer(app)和http.Server(app)?http 文档没有帮助。
采纳答案by mscdex
There is no difference. http.createServer()only does one thing: it calls http.Server()internally and returns the resulting instance.
没有区别。http.createServer()只做一件事:它在http.Server()内部调用并返回结果实例。

