http.createServer(app)v。http.Server(app) [英] http.createServer(app) v. http.Server(app)

查看:901
本文介绍了http.createServer(app)v。http.Server(app)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在socket.io网页上,开始:聊天应用程序,位于此处:

On the socket.io web page, Get Started: Chat application, located here:

http://socket.io/get-started/chat/

有这个代码:

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

socket.io示例使用http.Server()创建服务器。然而, app.listen()的快速文档显示了使用<$ c $创建服务器的示例c> http.createServer(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):


app.listen()

绑定并监听给定主机和端口上的连接。这个
方法与节点的http.Server#listen()相同。

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

由express()返回的应用程序实际上是一个JavaScript函数,
旨在作为回调传递到节点的HTTP服务器来处理
请求。这样,您可以轻松地向应用程序提供
的HTTP和HTTPS版本的
,因为应用程序不会从这些继承
(这只是一个回调):

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

app.listen()方法是以下的便利方法(如果
你希望使用HTTPS或提供两者,使用上述技术):

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


http.createServer有什么区别? (app) http.Server(app) ?? http文档没有帮助。

What's the difference between http.createServer(app) and http.Server(app)?? The http docs are no help.

推荐答案

没有区别。 http.createServer()只做一件事:它在内部调用 http.Server()并返回生成的实例

There is no difference. http.createServer() only does one thing: it calls http.Server() internally and returns the resulting instance.

这篇关于http.createServer(app)v。http.Server(app)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆