js-bson 错误 - OpenShift 上的 Mosca(MQTT 代理)

2024-02-02

我一直在 OpenShift 上使用 NodeJS 进行一些工作,当我尝试在 Node 实例中运行 Mosca 服务器时遇到问题。我收到的错误如下:

[Error: /var/lib/openshift/5547bd284382ec394a000088/app-root/runtime/repo/node_modules/mosca/node_modules/mongodb/node_modules/mongodb-core/node_modules/bson/node_modules/bson-ext/build/Release/bson.node: invalid ELF header]
js-bson: Failed to load c++ bson extension, using pure JS version

我最近使用 NodeJS 盒从头开始创建了一个新应用程序(在 OpenShift 上),并从盒中克隆了示例应用程序 git 存储库作为起点。我通过 npm 添加了 mosca (npm install mosca --save)。甚至没有使用 Mongo 作为 Mosca 的持久存储,所以只是在内存中运行,当我运行应用程序时,我收到了上面显示的错误。据我所知,当我在笔记本电脑上运行它时,一切都很好,所有版本都相同。

我已经尝试过在 Stack Overflow 上找到的一些方法,例如在将代码推回到 git 之前构建所有依赖模块并清理 npm 缓存,但没有效果。

如果有人对其他模块有类似的经验或对 Mosca 有相同的经验,并且可能能够为我指明正确的方向,我将不胜感激。

亲切的问候。

下面的代码是标准的 Node 示例应用程序模板,在启动函数中有一个示例 Mosca 服务器。

#!/bin/env node
//  OpenShift sample Node application
var express = require('express');
var fs      = require('fs');
var mosca   = require('mosca');

/**
 *  Define the sample application.
 */
var SampleApp = function() {

    //  Scope.
    var self = this;

    /*  ================================================================  */
    /*  Helper functions.                                                 */
    /*  ================================================================  */

    /**
     *  Set up server IP address and port # using env variables/defaults.
     */
    self.setupVariables = function() {
        //  Set the environment variables we need.
        self.ipaddress = process.env.OPENSHIFT_NODEJS_IP;
        self.port      = process.env.OPENSHIFT_NODEJS_PORT || 8080;

        if (typeof self.ipaddress === "undefined") {
            //  Log errors on OpenShift but continue w/ 127.0.0.1 - this
            //  allows us to run/test the app locally.
            console.warn('No OPENSHIFT_NODEJS_IP var, using 127.0.0.1');
            self.ipaddress = "127.0.0.1";
        };
    };


    /**
     *  Populate the cache.
     */
    self.populateCache = function() {
        if (typeof self.zcache === "undefined") {
            self.zcache = { 'index.html': '' };
        }

        //  Local cache for static content.
        self.zcache['index.html'] = fs.readFileSync('./index.html');
    };


    /**
     *  Retrieve entry (content) from cache.
     *  @param {string} key  Key identifying content to retrieve from cache.
     */
    self.cache_get = function(key) { return self.zcache[key]; };


    /**
     *  terminator === the termination handler
     *  Terminate server on receipt of the specified signal.
     *  @param {string} sig  Signal to terminate on.
     */
    self.terminator = function(sig){
        if (typeof sig === "string") {
           console.log('%s: Received %s - terminating sample app ...',
                       Date(Date.now()), sig);
           process.exit(1);
        }
        console.log('%s: Node server stopped.', Date(Date.now()) );
    };


    /**
     *  Setup termination handlers (for exit and a list of signals).
     */
    self.setupTerminationHandlers = function(){
        //  Process on exit and signals.
        process.on('exit', function() { self.terminator(); });

        // Removed 'SIGPIPE' from the list - bugz 852598.
        ['SIGHUP', 'SIGINT', 'SIGQUIT', 'SIGILL', 'SIGTRAP', 'SIGABRT',
         'SIGBUS', 'SIGFPE', 'SIGUSR1', 'SIGSEGV', 'SIGUSR2', 'SIGTERM'
        ].forEach(function(element, index, array) {
            process.on(element, function() { self.terminator(element); });
        });
    };


    /*  ================================================================  */
    /*  App server functions (main app logic here).                       */
    /*  ================================================================  */

    /**
     *  Create the routing table entries + handlers for the application.
     */
    self.createRoutes = function() {
        self.routes = { };

        self.routes['/asciimo'] = function(req, res) {
            var link = "http://i.imgur.com/kmbjB.png";
            res.send("<html><body><img src='" + link + "'></body></html>");
        };

        self.routes['/'] = function(req, res) {
            res.setHeader('Content-Type', 'text/html');
            res.send(self.cache_get('index.html') );
        };
    };


    /**
     *  Initialize the server (express) and create the routes and register
     *  the handlers.
     */
    self.initializeServer = function() {
        self.createRoutes();
        self.app = express();

        //  Add handlers for the app (from the routes).
        for (var r in self.routes) {
            self.app.get(r, self.routes[r]);
        }
    };


    /**
     *  Initializes the sample application.
     */
    self.initialize = function() {
        self.setupVariables();
        self.populateCache();
        self.setupTerminationHandlers();

        // Create the express server and routes.
        self.initializeServer();
    };


    /**
     *  Start the server (starts up the sample application).
     */
    self.start = function() {
        //  Start the app on the specific interface (and port).
        self.app.listen(self.port, self.ipaddress, function() {
            console.log('%s: Node server started on %s:%d ...',
                        Date(Date.now() ), self.ipaddress, self.port);
        });

        // START MOSCA SERVER

            var server = new mosca.Server({});

            server.on('clientConnected', function(client) {
                console.log('client connected', client.id);
            });

            // fired when a message is received
            server.on('published', function(packet, client) {
              console.log('Published', packet.payload);
            });

            server.on('ready', function() {
                console.log('Mosca server is up and running');
            });

    };

};   /*  Sample Application.  */



/**
 *  main():  Main code.
 */
var zapp = new SampleApp();
zapp.initialize();
zapp.start();

无效的 ELF 头通常意味着您尝试执行的二进制文件是在与您尝试运行它的系统不兼容的系统上编译的(在 x86 上编译,在 x86_64 上运行等)。您是否将应用程序中的 node_modules 目录提交到 git ?您应该让 OpenShift 应用程序为您安装正确的节点模块,而不是提交该目录/将其添加到 git 版本控制系统中。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

js-bson 错误 - OpenShift 上的 Mosca(MQTT 代理) 的相关文章

随机推荐