简体   繁体   English

初学者对Express和socket.io中的模块结构感到困惑

[英]Beginner confused about module structure in Express and socket.io

All: 所有:

I am new to Node.js, when I learn integrating Socket.io into Express.js, there is one question about modulization confused me so much: 我是Node.js的新手,当我学习将Socket.io集成到Express.js中时,有一个关于模块化的问题让我如此困惑:

I use default Express.js project structure, main js files involve my confuse are: 我使用默认的 Express.js项目结构,主要的js文件涉及到我的困惑是:

app.js: define request handler and use module.exports = app; app.js:定义请求处理程序并使用module.exports = app; as a module. 作为一个模块。

bin/www: require app.js, setup and configure http server. bin / www: require app.js,设置和配置http服务器。

Everything seems modularized great. 一切看起来模块化都很棒。

But when it comes to Socket.io integration, this structure begins to confuse me: 但是当谈到Socket.io集成时,这种结构开始让我困惑:

From Socket.io Docs: 来自Socket.io文档:

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

server.listen(80);

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

io.on('connection', function (socket) {
  socket.emit('news', { hello: 'world' });
  socket.on('my other event', function (data) {
    console.log(data);
  });
});

Since socket.io needs server variabe, so I figure out this code should be in www file, however I also want to modularize the io event handling part into a module, but I do not know how to export that io variable to www since it need a variable server from www. 由于socket.io需要服务器变量,所以我发现这个代码应该在www文件中,但是我也想将io事件处理部分模块化为模块,但我不知道如何将io变量导出到www,因为它需要一个来自www的可变服务器。

I wonder: does that mean I have to keep all socketio event handling code in www file? 我想知道:这是否意味着我必须将所有socketio事件处理代码保存在www文件中? Could anyone give me some code to show how to modularize this? 谁能给我一些代码来展示如何模块化这个?

PS: Thanks for passing by and giving a down vote for this which prove that this is a entry level question or worthless, however, please leave your answer to this with your down vote, because it is really important to entry level beginner like me. PS:感谢你过去并给予了一次投票,这证明这是一个入门级问题或者毫无价值,但是,请用你的投票留下你的答案,因为这对像我这样的入门级初学者来说非常重要。

Thanks 谢谢

The organization of socket code is not necessarily intuitive but you could think of it as similar to the http side of things and the way that functions are often provided to routes. 套接字代码的组织不一定是直观的,但你可以认为它类似于事物的http方面以及通常为路由提供函数的方式。

Notice that you are just providing a function to "io.on". 请注意,您只是为“io.on”提供了一个功能。 So, you could do something like this. 所以,你可以做这样的事情。 In a separate file create a module for your socket functions... 在单独的文件中为您的套接字函数创建一个模块......

// File named socket_funcs.js
module.exports.connection = function (socket) {
    socket.emit('news', { hello: 'world' });
    socket.on('my other event', function (data) {
    console.log(data);
});

Then in your app.js file you can do this... 然后在你的app.js文件中你可以这样做......

var app = require('express')();
var server = require('http').Server(app);
var io = require('socket.io')(server);
var sf = require('./socket_funcs.js');

server.listen(80);

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

io.on('connection', sf.connection);

I did not down vote your question. 我没有投票给你的问题。 It seems an appropriate enough question for a beginner. 这对初学者来说似乎是一个恰当的问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM