简体   繁体   English

将Node.js HTTP服务器对象传递给Socket.IO时,“对象不是函数”

[英]“Object is not a function” when passing a Node.js HTTP server object to Socket.IO

This was working a few months ago when I was creating an HTTPS server, but I switched to http (not sure this switch is directly related, just mentioning it in case) today when revisiting this application, where I create a server and pass it to socket.io: 几个月前,当我创建一个HTTPS服务器时,这个工作正在运行,但今天我重新访问这个应用程序时切换到http(不确定这个交换机是否直接相关,只是提到它),我创建了一个服务器并将其传递给socket.io:

init.js

var server      = require(dirPath + "/custom_modules/server").serve(80); 
var socket      = require(dirPath + "/custom_modules/socket").socket(server);

It is important that I pass the server to socket.io (I know there are alternate ways of initializing the socket) this way because that's how it has to be done in order to encrypt the websocket connection when I switch back to serving HTTPS later. 重要的是我将服务器传递给socket.io(我知道有其他方法可以初始化套接字),这是因为当我稍后切换回服务HTTPS时,为了加密websocket连接必须要这样做。

So my server module: 所以我的服务器模块:

//serve files
module.exports.serve = function(port) {
    //var server = https.createServer(options, function(req, res) { // SSL Disabled
    var server = http.createServer(function(req, res) { 

        // Parse & process URL
        var reqInfo = url.parse(req.url, true, true), path = reqInfo.pathname;

        // Quickly handle preloaded requests
        if (preloaded[path])
            preloadReqHandler(req, res, preloaded[path], path);

        // Handle general requests
        else
            generalReqHandler(req, res, reqInfo);
    }).listen(port);
    return server; //this should be returning an http server object for socket.io
};

and my socket module: 和我的套接字模块:

module.exports.socket = function(server) {

    //create socket
    var socket = require(dirPath + '/node_modules/socket.io')(server);
    //                                                       ^ error

    // .. snip ..

    //handle client connection
    socket.on("connection", function(client) {
        // .. snip ..
    });
};

and my error: 和我的错误:

 /home/ec2-user/Sales_Freak/server/custom_modules/socket.js:17 var socket = require(dirPath + '/node_modules/socket.io')(server); ^ TypeError: object is not a function at Object.module.exports.socket (/home/ec2-user/Sales_Freak/server/custom_modules/socket.js:17:59) at Object.<anonymous> (/home/ec2-user/Sales_Freak/server/init.js:16:59) 

Assume all of the necessary Node.JS modules are required properly above. 假设上面正确地需要所有必需的Node.JS模块。 What silly mistake am I making today? 我今天犯的是什么愚蠢的错误?

The exported module is not a function, refer to your previous statement: 导出的模块不是函数,请参考之前的语句:

var socket      = require(dirPath + "/custom_modules/socket").socket(server);

And compare that to your current statement: 并将其与您当前的陈述进行比较:

var socket = require(dirPath + '/node_modules/socket.io')(server);

I think you meant to do this instead. 我认为你的意思是这样做。

var socket = require(dirPath + '/node_modules/socket.io').socket(server);

This might or might not be helpful to others, but my problem was that I changed the directory of my Node.js server files and socket.io wasn't installed in the new location. 这可能对其他人有帮助,也可能没有用,但我的问题是我更改了Node.js服务器文件的目录,而socket.io没有安装在新位置。

The module was there in node_modules but not installed. 模块位于node_modules但未安装。 I'm actually not sure how installation works with npm modules, but the module existed and therefore didnt throw an error saying it didnt exist, but did not act like it was really there until I did npm install socket.io 我实际上不确定安装是如何与npm模块一起工作的,但是模块存在,因此没有抛出一个错误,说它不存在,但是在我执行npm install socket.io之前没有表现得像它真的那样

If you get this error in this situation, you forgot install socket.io. 如果在这种情况下出现此错误,则忘记安装socket.io。

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

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