使用node.js require加载模块

时间:2022-09-04 09:57:34

I'm building a node.js app using express and jade frameworks. The app has the standard skeleton, that's how it looks the main directory :

我正在使用express和jade框架构建node.js应用程序。该应用程序具有标准骨架,它是主目录的外观:

  • app.js
  • bin
  • npm-debug.log
  • views
  • node_modules
  • package.json
  • public

The problem occurs when I try to load the socket.io module in a js file (called lets say x.js) within the public directory. That's where I've implemented some logic tightly connected to user-action event handling. So in other words when someone clicks on a button 'connect', I'd like to establish connection using socket.io. The problem is that when I add this line

当我尝试在公共目录中的js文件(称为x.js)中加载socket.io模块时,会出现此问题。这就是我实现了一些与用户操作事件处理紧密相关的逻辑。换句话说,当有人点击“connect”按钮时,我想使用socket.io建立连接。问题是当我添加这一行时

var socket_io = require('socket.io'); in x.js (x.js is located in the /public dir)

var socket_io = require('socket.io');在x.js中(x.js位于/ public目录中)

the whole functionality suddenly stops working, I guess due to the fact that the module is not loaded although the var socket_io isn't used anywhere below within the x.js file. If I add the line var socket_io = require('socket.io'); to app.js everything works. I looked into this SO question but with no success. Can someone explain why is this happening and what am I doing wrong?

整个功能突然停止工作,我猜是因为虽然在下面x.js文件中的任何地方都没有使用var socket_io,但模块没有加载。如果我添加行var socket_io = require('socket.io'); app.js一切正常。我调查了这个问题,但没有成功。有人可以解释为什么会发生这种情况以及我做错了什么?

Edit: Just to clarify that I've added 'socket.io' to the package.json file. Also installed socket.io both globally and locally using npm install (-g) socket.io command.

编辑:只是为了澄清我已经将'socket.io'添加到package.json文件中。还使用npm install(-g)socket.io命令在全局和本地安装了socket.io。

2 个解决方案

#1


To init socket.io you need some crucial things, like the server var that is init in app.js file. So, if you try to require socket.io out of app.js and you try to init socket.io, then you could have some troubles. Because socket.io can't be init without the server var in your case.

要初始化socket.io,您需要一些关键的东西,比如在app.js文件中为init的服务器var。所以,如果你试图从app.js中要求socket.io并尝试初始化socket.io,那么你可能会有一些麻烦。因为socket.io不能在没有服务器var的情况下初始化。

If you try to require socket.io and not init it. I think you will have not problem. So try to understand the section that is related to Express in the socket.io documentation : http://socket.io/docs/#

如果您尝试要求socket.io而不是init。我想你不会有问题。因此,请尝试在socket.io文档中了解与Express相关的部分:http://socket.io/docs/#

Then you can try to use the npm module for express and socket.io : http://express-io.org/

然后你可以尝试使用npm模块进行express和socket.io:http://express-io.org/

Or you can deal with socket.of() method : http://socket.io/docs/rooms-and-namespaces/#

或者你可以处理socket.of()方法:http://socket.io/docs/rooms-and-namespaces/#

In any event, you should init socket.io with the server var. So, you can make your module to manage socket.io behavior, define some methods, etc. For this purpose, you can passe socket.io in argument to your route files. See here :Use socket.io inside a express routes file Then, that logic allow you to use socket.io anywhere in your app.

无论如何,你应该使用服务器var初始化socket.io。因此,您可以使模块管理socket.io行为,定义一些方法等。为此,您可以将socket.io传递给路径文件的参数。请参阅此处:在快速路由文件中使用socket.io然后,该逻辑允许您在应用程序的任何位置使用socket.io。

#2


This is not an attempt to answer the question asked but a further question put to me in the commments. Ideally I would have answered it in the comments but felt a visual aspect could explain better.
使用node.js require加载模块
I have a folder structure like this in my app, an MVC type structure. The controllers/ folder contains the business logic. The models/ folder contains my models. The routes folder contains routes/ and the views/ folder contains my jade templates.

这不是试图回答所提出的问题,而是在文章中向我提出了另一个问题。理想情况下,我会在评论中回答它,但觉得视觉方面可以更好地解释。我的应用程序中有一个像这样的文件夹结构,MVC类型结构。 controllers /文件夹包含业务逻辑。 models /文件夹包含我的模型。 routes文件夹包含routes /,views /文件夹包含我的jade模板。

#1


To init socket.io you need some crucial things, like the server var that is init in app.js file. So, if you try to require socket.io out of app.js and you try to init socket.io, then you could have some troubles. Because socket.io can't be init without the server var in your case.

要初始化socket.io,您需要一些关键的东西,比如在app.js文件中为init的服务器var。所以,如果你试图从app.js中要求socket.io并尝试初始化socket.io,那么你可能会有一些麻烦。因为socket.io不能在没有服务器var的情况下初始化。

If you try to require socket.io and not init it. I think you will have not problem. So try to understand the section that is related to Express in the socket.io documentation : http://socket.io/docs/#

如果您尝试要求socket.io而不是init。我想你不会有问题。因此,请尝试在socket.io文档中了解与Express相关的部分:http://socket.io/docs/#

Then you can try to use the npm module for express and socket.io : http://express-io.org/

然后你可以尝试使用npm模块进行express和socket.io:http://express-io.org/

Or you can deal with socket.of() method : http://socket.io/docs/rooms-and-namespaces/#

或者你可以处理socket.of()方法:http://socket.io/docs/rooms-and-namespaces/#

In any event, you should init socket.io with the server var. So, you can make your module to manage socket.io behavior, define some methods, etc. For this purpose, you can passe socket.io in argument to your route files. See here :Use socket.io inside a express routes file Then, that logic allow you to use socket.io anywhere in your app.

无论如何,你应该使用服务器var初始化socket.io。因此,您可以使模块管理socket.io行为,定义一些方法等。为此,您可以将socket.io传递给路径文件的参数。请参阅此处:在快速路由文件中使用socket.io然后,该逻辑允许您在应用程序的任何位置使用socket.io。

#2


This is not an attempt to answer the question asked but a further question put to me in the commments. Ideally I would have answered it in the comments but felt a visual aspect could explain better.
使用node.js require加载模块
I have a folder structure like this in my app, an MVC type structure. The controllers/ folder contains the business logic. The models/ folder contains my models. The routes folder contains routes/ and the views/ folder contains my jade templates.

这不是试图回答所提出的问题,而是在文章中向我提出了另一个问题。理想情况下,我会在评论中回答它,但觉得视觉方面可以更好地解释。我的应用程序中有一个像这样的文件夹结构,MVC类型结构。 controllers /文件夹包含业务逻辑。 models /文件夹包含我的模型。 routes文件夹包含routes /,views /文件夹包含我的jade模板。