我是nodejs的新手,想要从tokbox服务构建api。 我想部署,但失败,本地是正常的工作。 Heroku和openshift都得到503错误。 你能帮忙解决这个问题吗? thakns
// Dependencies
var express = require('express'),
OpenTok = require('opentok');
var server_port = process.env.OPENSHIFT_NODEJS_PORT || 3000
// var server_ip_address = process.env.OPENSHIFT_NODEJS_IP || '127.0.0.1'
// Verify that the API Key and API Secret are defined
var apiKey = "***",
apiSecret = "*****";
if (!apiKey || !apiSecret) {
console.log('You must specify API_KEY and API_SECRET environment variables');
process.exit(1);
}
// Initialize the express app
var app = express();
app.use(express.static(__dirname + '/public'));
// Initialize OpenTok
var opentok = new OpenTok(apiKey, apiSecret);
// Create a session and store it in the express app
opentok.createSession({ mediaMode: 'routed' },function(err, session) {
if (err) throw err;
app.set('sessionId', session.sessionId);
// We will wait on starting the app until this is done
init();
});
app.get('/', function(req, res) {
res.render('index.ejs');
});
app.get('/host', function(req, res) {
var sessionId = app.get('sessionId'),
// generate a fresh token for this client
token = opentok.generateToken(sessionId, { role: 'moderator' });
res.json('host.ejs', {
apiKey: apiKey,
sessionId: sessionId,
token: token
});
});
app.get('/participant', function(req, res) {
var sessionId = app.get('sessionId'),
// generate a fresh token for this client
token = opentok.generateToken(sessionId, { role: 'moderator' });
res.json('participant.ejs', {
apiKey: apiKey,
sessionId: sessionId,
token: token
});
});
app.get('/history', function(req, res) {
var page = req.param('page') || 1,
offset = (page - 1) * 5;
opentok.listArchives({ offset: offset, count: 5 }, function(err, archives, count) {
if (err) return res.send(500, 'Could not list archives. error=' + err.message);
res.json('history.ejs', {
archives: archives,
showPrevious: page > 1 ? ('/history?page='+(page-1)) : null,
showNext: (count > offset + 5) ? ('/history?page='+(page+1)) : null
});
});
});
app.get('/download/:archiveId', function(req, res) {
var archiveId = req.param('archiveId');
opentok.getArchive(archiveId, function(err, archive) {
if (err) return res.send(500, 'Could not get archive '+archiveId+'. error='+err.message);
res.redirect(archive.url);
});
});
app.get('/start', function(req, res) {
opentok.startArchive(app.get('sessionId'), {
name: 'Node Archiving Sample App'
}, function(err, archive) {
if (err) return res.send(500,
'Could not start archive for session '+sessionId+'. error='+err.message
);
res.json(archive);
});
});
app.get('/stop/:archiveId', function(req, res) {
var archiveId = req.param('archiveId');
opentok.stopArchive(archiveId, function(err, archive) {
if (err) return res.send(500, 'Could not stop archive '+archiveId+'. error='+err.message);
res.json(archive);
});
});
app.get('/delete/:archiveId', function(req, res) {
var archiveId = req.param('archiveId');
opentok.deleteArchive(archiveId, function(err) {
if (err) return res.send(500, 'Could not stop archive '+archiveId+'. error='+err.message);
res.redirect('/history');
});
});
// Start the express app
function init() {
app.listen(server_port, function() {
// console.log('You\'re app is now ready at http://localhost:3000/');
console.log( "Listening on " + ", server_port " + server_port )
});
}
的package.json
{
"name": "opentok-archiving-sample",
"version": "0.0.0",
"description": "Demo of OpenTok API",
"scripts": {
"start": "node index.js"
},
"main": "index.js",
"author": "",
"license": "ISC",
"dependencies": {
"cloud-env": "^0.1.0",
"ejs": "^0.8.6",
"express": "^3.5.0",
"opentok": "^2.2.4",
"package": "^1.0.1",
"package.json": "0.0.0"
},
"engines": {
"node": "0.10.32"
}
}
Procfile
web: node index.js
heroku日志
2014-11-25T14:57:19.510550+00:00 heroku[web.1]: State changed from crashed to starting
2014-11-25T14:57:22.210900+00:00 heroku[web.1]: Starting process with command `node index.js`
2014-11-25T14:57:23.914960+00:00 app[web.1]: Listening on , server_port 3000
2014-11-25T14:58:22.632339+00:00 heroku[web.1]: Error R10 (Boot timeout) -> Web process failed to bind to $PORT within 60 seconds of launch
2014-11-25T14:58:22.633344+00:00 heroku[web.1]: Stopping process with SIGKILL
2014-11-25T14:58:23.594609+00:00 heroku[web.1]: State changed from crashed to starting
2014-11-25T14:58:23.593810+00:00 heroku[web.1]: State changed from starting to crashed
2014-11-25T14:58:23.584986+00:00 heroku[web.1]: Process exited with status 137
2014-11-25T14:58:25.481786+00:00 heroku[web.1]: Starting process with command `node index.js`
2014-11-25T14:58:27.589769+00:00 app[web.1]: Listening on , server_port 3000
2014-11-25T14:58:45.135278+00:00 heroku[router]: at=error code=H20 desc="App boot timeout" method=GET path="/" host=infinite-gorge-6020.herokuapp.com request_id=65a70579-fe23-42a8-8aca-04657ace27c5 fwd="54.91.142.166" dyno= connect= service= status=503 bytes=
答案 0 :(得分:1)
查看您的Heroku日志,似乎您绑定到端口3000而不是通过PORT
环境变量提供的端口(请参阅Heroku docs)。将第4行更改为此行:
var server_port = process.env.PORT || 3000;