我对此非常陌生。这是我第一次尝试在生产环境中部署应用程序。它在本地完全正常工作(当然还有本地环境变量而不是Openshift的环境变量)。但是在openshift上,我无法让它运行起来。它始终响应" 503服务暂时不可用"。我几个小时都在修补它,并且到处寻找解决方案,但没有任何工作。
这是我在index.js文件中的内容(它只是一个简单的mongodb利用待办事项列表。就像我说的,它在本地工作正常,但是,是的,它'非常混乱。就像我说的那样。我是新的):
var path = require('path');
var express = require('express');
var bodyParser = require('body-parser');
var MongoClient = require('mongodb').MongoClient;
var ObjectId = require('mongodb').ObjectId;
var methodOverride = require('method-override');
var session = require('express-session');
var app = express();
var router = express.Router();
var dbUrl = 'mongodb://$OPENSHIFT_MONGODB_DB_HOST:$OPENSHIFT_MONGODB_DB_PORT/todo_list';
var result;
var list;
var port = process.env.OPENSHIFT_NODEJS_PORT;
app.use(bodyParser.urlencoded({ extended: false }));
app.use(methodOverride(function(req, res){
if (req.body && typeof req.body === 'object' && '_method' in req.body) {
// look in urlencoded POST bodies and delete it
var method = req.body._method
delete req.body._method
return method
}
}));
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'jade');
app.use(express.static(path.join(__dirname, 'public')));
function updateList(res){
MongoClient.connect(dbUrl, function(err, db){
if(err){console.log(err);}
var list = db.collection('list');
list.find({}).toArray(function(err, docs){
res.render('index', { title: 'To do:', list: docs });
});
});
};
//routes
app.get('/', function(req, res){
updateList(res);
});
app.post('/', function(req, res){
var data = req.body.words;
MongoClient.connect(dbUrl, function(err, db){
var list = db.collection('list');
list.insert({ task: data }, updateList(res));
});
});
app.delete('/:id', function(req, res){
var removal = req.params.id;
MongoClient.connect(dbUrl, function(err, db){
var list = db.collection('list');
list.remove({ _id: ObjectId(removal)}, updateList(res));
});
res.redirect('/');
});
var server = app.listen(port, function(){
console.log('listening');
});
module.exports = server;
接下来是我注销的内容:
Error: listen EACCES
at errnoException (net.js:901:11)
at Server._listen2 (net.js:1020:19)
at listen (net.js:1061:10)
at Server.listen (net.js:1135:5)
at EventEmitter.listen (/var/lib/openshift/<key>/app-root/runtime/repo/node_modules/express/lib/application.js:617:24)
at Object.<anonymous> (/var/lib/openshift/<key>/app-root/runtime/repo/index.js:66:18)
at Module._compile (module.js:456:26)
at Object.Module._extensions..js (module.js:474:10)
at Module.load (module.js:356:32)
at Function.Module._load (module.js:312:12)
DEBUG: Program node index.js exited with code 8
DEBUG: Starting child process with 'node index.js'
我不知所措。请帮忙。
答案 0 :(得分:1)
您还需要绑定到OPENSHIFT_NODEJS_IP环境变量。这可以在app.listen的第二个参数中完成,如下所示:
var ipAddress = process.env.OPENSHIFT_NODEJS_IP
var server = app.listen(port, ipAddress, function(){
console.log('listening');
});
如果你不这样做,默认的IP地址将是0.0.0.0,这在Openshift上不起作用