本文翻译自:Express.js - app.listen vs server.listen
This may be a very basic question but I simply don't get it. 这可能是一个非常基本的问题,但我根本不明白。 What is the difference between creating an app using Express.js and 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
,则得到相同的输出。
#1楼
参考:https://stackoom.com/question/1CFkH/Express-js-app-listen与server-listen
#2楼
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.io
within the same HTTP server instance: 如果您想重用HTTP服务器,例如在同一HTTP服务器实例中运行socket.io
,则第二种形式(自己创建一个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) {
...
});
#3楼
There is one more difference of using the app and listening to http server is when you want to setup for https server 当您要为https服务器设置时,使用该应用程序和收听http服务器还有另外一个区别
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中的应用程序将仅返回http服务器,您无法在express中设置它,因此需要使用https server命令
var express = require('express');
var app = express();
app.listen(1234);
#4楼
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. 他们可以设置中间件以使用express轻松响应HTTP请求。
- They can dynamically render HTML Pages based on passing arguments to templates using express. 他们可以使用express将参数传递给模板,从而动态呈现HTML页面。
- They can also define routing easily using express. 他们还可以使用express轻松定义路由。
#5楼
I came with same question but after google, I found there is no big difference :) 我遇到了同样的问题,但是在谷歌之后,我发现并没有太大的区别:)
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,请参见他们的示例
I prefer app.listen()
:) 我更喜欢app.listen()
:)
#6楼
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 PASSED to Node's HTTP servers as a callback to handle requests. 通过快递()返回该应用程序实际上是一个JavaScript功能,旨在传递到节点的HTTP服务器作为一个回调来处理请求。
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): 这使为应用程序的HTTP和HTTPS版本提供相同的代码库变得容易,因为该应用程序不继承自这些代码(它只是一个回调):
http.createServer(app).listen(80);
https.createServer(options, app).listen(443);
The app.listen() method returns an http.Server object and (for HTTP) is a convenience method for the following: app.listen()方法返回http.Server对象,并且(对于HTTP)是用于以下目的的便捷方法 :
app.listen = function() {
var server = http.createServer(this);
return server.listen.apply(server, arguments);
};