我在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(npm install mosca --save
)添加了mosca。甚至没有使用Mongo作为Mosca的持久存储,所以只是在内存中运行,我在运行应用程序时得到上面显示的错误。我可以在我的笔记本电脑上运行它,相同版本的所有内容都很好。据我所知。
我已经尝试了一些我在Stack Overflow上发现的东西,例如在将代码推回git并清理npm缓存之前构建所有依赖模块但是无济于事。
如果有人与其他模块有过类似的经历或者与Mosca有相同的经历,我可能会感激,并且可以指出我正确的方向。
亲切的问候。
下面的代码是标准的Node示例应用程序模板,在start函数中有一个示例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();
答案 0 :(得分:1)
无效的ELF标头通常意味着您尝试执行的二进制文件是在与您尝试运行它的系统不兼容的系统上编译的(在x86上编译,在x86_64上运行等)。您是否将应用程序中的node_modules目录提交给git?您应该让OpenShift应用程序为您安装正确的节点模块,而不是提交该目录/将其添加到您的git版本控制系统中。