What is happening when require("http").Server() is evaluated with an Express app as its argument?

Alex Booth

I was reading the Socket.io Chat Demo here: http://socket.io/get-started/chat/ and I got confused when looking at their require statements.

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

app.get('/', function(req, res){
res.sendfile('index.html');
});

io.on('connection', function(socket){
console.log('a user connected');
});

http.listen(3000, function(){
console.log('listening on *:3000');
});

Am I correct in thinking that require("express") produces an executable Express function (with all of the necessary functions and fields that go with that), and that require("http").Server(app) creates a http.Server object with all of its fields and functions.

If so, I am confused because Express creates a server when we call the .listen function so it seems redundant and backwards to pass an Express app to an http module server.

So, my question is, what is really happening here?

Timothy Strimple

The http server expects a function which has the following signature:

function(req, res)

require('express')(); will create a function with that signature which handles all the magic that express makes available like routing, middleware, etc. Express can create it's own http server instance, but since you're also using socket.io (which expects access to the http server as well), you'll need a separate http instance.

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

以Express应用程序作为参数评估require(“ http”)。Server()时会发生什么情况?

来自分类Dev

API calls from the server require an appsecret_proof argument

来自分类Dev

var express = require('express'); var app = express(),什么是express()??是方法还是构造函数?它从何而来

来自分类Dev

http.createServer(app)诉http.Server(app)

来自分类Dev

“ var app = require('express')()”中的第二个括号是做什么的?

来自分类Dev

如何运行 require("express"); 来自 Electron App 中的 TypeScript Angular 组件

来自分类Dev

在node.js / express中需要http.createServer(app)

来自分类Dev

如何从Express应用程序获取http.server?

来自分类Dev

On the SQL tab what is happening during the Offset time?

来自分类Dev

what the cause of exception when connecting to the server

来自分类Dev

what the cause of exception when connecting to the server

来自分类Dev

Function that just returns its argument?

来自分类Dev

React Native和require('http')

来自分类Dev

What is the express.js equivalent of the node.js http module's "req.url"?

来自分类Dev

节点新手-require('express')的目的

来自分类Dev

节点新手-require('express')的目的

来自分类Dev

在使用require('express')时显示错误

来自分类Dev

将Express App部署到Azure App Services没有响应端口8080上的HTTP ping

来自分类Dev

Adding custom properties to Express app and req. What's the best way?

来自分类Dev

我的handler.js文件中未定义Express Server处理程序“ app”

来自分类Dev

为什么Express Server侦听的端口与我的React App所在的端口不同

来自分类Dev

CRUD nodejs/express server: app.put req.body 为空

来自分类Dev

what is the purpose of db=0 when connecting to redis server in python?

来自分类Dev

How do I force SSL when running an express app behind Apache?

来自分类Dev

SQL Server Express维护

来自分类Dev

SQL Server Express备份

来自分类Dev

sql server express权限

来自分类Dev

SQL Server Express 复制

来自分类Dev

使用Express过滤HTTP响应

Related 相关文章

  1. 1

    以Express应用程序作为参数评估require(“ http”)。Server()时会发生什么情况?

  2. 2

    API calls from the server require an appsecret_proof argument

  3. 3

    var express = require('express'); var app = express(),什么是express()??是方法还是构造函数?它从何而来

  4. 4

    http.createServer(app)诉http.Server(app)

  5. 5

    “ var app = require('express')()”中的第二个括号是做什么的?

  6. 6

    如何运行 require("express"); 来自 Electron App 中的 TypeScript Angular 组件

  7. 7

    在node.js / express中需要http.createServer(app)

  8. 8

    如何从Express应用程序获取http.server?

  9. 9

    On the SQL tab what is happening during the Offset time?

  10. 10

    what the cause of exception when connecting to the server

  11. 11

    what the cause of exception when connecting to the server

  12. 12

    Function that just returns its argument?

  13. 13

    React Native和require('http')

  14. 14

    What is the express.js equivalent of the node.js http module's "req.url"?

  15. 15

    节点新手-require('express')的目的

  16. 16

    节点新手-require('express')的目的

  17. 17

    在使用require('express')时显示错误

  18. 18

    将Express App部署到Azure App Services没有响应端口8080上的HTTP ping

  19. 19

    Adding custom properties to Express app and req. What's the best way?

  20. 20

    我的handler.js文件中未定义Express Server处理程序“ app”

  21. 21

    为什么Express Server侦听的端口与我的React App所在的端口不同

  22. 22

    CRUD nodejs/express server: app.put req.body 为空

  23. 23

    what is the purpose of db=0 when connecting to redis server in python?

  24. 24

    How do I force SSL when running an express app behind Apache?

  25. 25

    SQL Server Express维护

  26. 26

    SQL Server Express备份

  27. 27

    sql server express权限

  28. 28

    SQL Server Express 复制

  29. 29

    使用Express过滤HTTP响应

热门标签

归档