gpt4 book ai didi

node.js - http.createServer(app) 诉 http.Server(app)

转载 作者:IT老高 更新时间:2023-10-28 21:56:12 24 4
gpt4 key购买 nike

在 socket.io 网页上,Get Started: Chat application,位于此处:

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

有这个代码:

var app = require('express')();
var http = require('http').Server(app);

可以改写得更清楚一点:

var express = require('express');
var http = require('http');

var app = express();
var server = http.Server(app);

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

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

最佳答案

没有区别。 http.createServer() 只做一件事:it calls http.Server() internally and returns the resulting instance .

关于node.js - http.createServer(app) 诉 http.Server(app),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26921117/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com