node.js Express.js - app.listen 与 server.listen
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17696801/
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
Express.js - app.listen vs server.listen
提问by Tamas
This may be a very basic question but I simply don't get it. What is the difference between creating an app using Express.jsand starting the app listening on port 1234, for example:
这可能是一个非常基本的问题,但我根本不明白。使用Express.js创建应用程序和启动应用程序侦听端口 1234之间有什么区别,例如:
var express = require('express');
var app = express();
//app.configure, app.use etc
app.listen(1234);
and adding an http server:
并添加一个 http 服务器:
var express = require('express');
var http = require('http');
var app = express();
var server = http.createServer(app);
//app.configure, app.use etc
server.listen(1234);
What's the difference?
If I navigate to http://localhost:1234, thus I get the same output.
有什么不同?
如果我导航到http://localhost:1234,则得到相同的输出。
回答by robertklep
The second form (creating an HTTP server yourself, instead of having Express create one for you) is useful if you want to reuse the HTTP server, for example to run socket.iowithin the same HTTP server instance:
如果您想重用 HTTP 服务器,例如socket.io在同一个 HTTP 服务器实例中运行,则第二种形式(自己创建一个 HTTP 服务器,而不是让 Express 为您创建一个)很有用:
var express = require('express');
var app = express();
var server = require('http').createServer(app);
var io = require('socket.io').listen(server);
...
server.listen(1234);
However, app.listen()also returns the HTTP server instance, so with a bit of rewriting you can achieve something similar without creating an HTTP server yourself:
但是,app.listen()也会返回 HTTP 服务器实例,因此通过一些重写,您可以实现类似的功能,而无需自己创建 HTTP 服务器:
var express = require('express');
var app = express();
// app.use/routes/etc...
var server = app.listen(3033);
var io = require('socket.io').listen(server);
io.sockets.on('connection', function (socket) {
...
});
回答by Tim
There is one more difference of using the app and listening to http server is when you want to setup for https server
使用应用程序和侦听 http 服务器的另一个区别是当您要设置 https 服务器时
To setup for https, you need the code below:
要设置 https,您需要以下代码:
var https = require('https');
var server = https.createServer(app).listen(config.port, function() {
console.log('Https App started');
});
The app from express will return http server only, you cannot set it in express, so you will need to use the https server command
express中的app只会返回http server,不能在express中设置,需要使用https server命令
var express = require('express');
var app = express();
app.listen(1234);
回答by Ivan Talalaev
Just for punctuality purpose and extend a bit Tim answer.
From official documentation:
只是为了守时的目的,并扩展一点蒂姆的答案。
来自官方文档:
The app returned by express() is in fact a JavaScript Function, DESIGNED TO BE PASSEDto Node's HTTP servers as a callback to handle requests.
This makes it easy to provide both HTTP and HTTPS versions of your app with the same code base, as the app does not inherit from these (it is simply a callback):
express() 返回的应用程序实际上是一个 JavaScript 函数, 设计为传递给 Node 的 HTTP 服务器作为处理请求的回调。
这使您可以轻松地使用相同的代码库同时提供 HTTP 和 HTTPS 版本的应用程序,因为该应用程序不会从这些版本继承(它只是一个回调):
http.createServer(app).listen(80);
https.createServer(options, app).listen(443);
The app.listen()method returns an http.Serverobject and (for HTTP) is a convenience methodfor the following:
所述app.listen()方法返回的http.Server对象和(对于HTTP)是一个方便的方法用于以下情况:
app.listen = function() {
var server = http.createServer(this);
return server.listen.apply(server, arguments);
};
回答by Muhammad Shahzad
I came with same question but after google, I found there is no big difference :)
我提出了同样的问题,但在谷歌之后,我发现没有太大区别:)
From Github
来自Github
If you wish to create both an HTTP and HTTPS server you may do so with the "http" and "https" modules as shown here.
如果您希望同时创建 HTTP 和 HTTPS 服务器,您可以使用“http”和“https”模块,如下所示。
/**
* Listen for connections.
*
* A node `http.Server` is returned, with this
* application (which is a `Function`) as its
* callback. If you wish to create both an HTTP
* and HTTPS server you may do so with the "http"
* and "https" modules as shown here:
*
* var http = require('http')
* , https = require('https')
* , express = require('express')
* , app = express();
*
* http.createServer(app).listen(80);
* https.createServer({ ... }, app).listen(443);
*
* @return {http.Server}
* @api public
*/
app.listen = function(){
var server = http.createServer(this);
return server.listen.apply(server, arguments);
};
Also if you want to work with socket.io see their example
另外,如果您想使用 socket.io,请参阅他们的示例
See this
看到这个
I prefer app.listen():)
我更喜欢app.listen():)
回答by Sarim Javaid Khan
Express is basically a wrapper of http module that is created for the ease of the developers in such a way that..
Express 基本上是一个 http 模块的包装器,它是为方便开发人员而创建的,其方式是..
- They can set up middlewares to respond to HTTP Requests (easily) using express.
- They can dynamically render HTML Pages based on passing arguments to templates using express.
- They can also define routing easily using express.
- 他们可以设置中间件以使用 express(轻松)响应 HTTP 请求。
- 他们可以基于使用 express 向模板传递参数来动态呈现 HTML 页面。
- 他们还可以使用 express 轻松定义路由。

