OpenShift NodeJS 应用程序 503 错误(端口问题?)
OpenShift NodeJS Application 503 Error (port issue?)
我正在尝试为学校项目创建一个 NodeJS 应用程序。基本思想是一个 NodeJS 服务器,它将处理对它的请求并跟踪具有特定格式的请求(这些将来自张贴在各个区域的二维码)。
我想使用 OpenShift 来托管应用程序,并且我可以正常构建它,但是每次我收到 URL 发布时它都会给我一个 503 错误,即使我使用了正确格式的请求(例如:psychexpserver-psychexp.rhcloud.com/MU-EllisLib-Blue-NoPhrase)。我已经在本地测试了该应用程序,它运行良好。我研究了这个主题,但没有找到适用于我的端口配置错误的答案吗?我应该去另一个 URL 吗?
代码:
var app = require('express')();
var http = require('http').Server(app);
var fs = require('fs');
// Port which we listen to - get from OpenShift
var serverPort = process.env.OPENSHIFT_NODEJS_PORT || 80;
var serverIp = process.env.OPENSHIFT_NODEJS_IP || '127.0.0.1';
app.set('port', serverPort);
app.set('ipaddr', serverIp);
// Data logging
app.get('/:val', function(req, res)
{
// Grab request url
var data = req.url;
// Process and write to file
processData(data);
// Redirect to follow up form
res.redirect('https://docs.google.com/forms/d/11JbF4FE_Vd1Dd6PD8wMbs4Kxc15GgA3NL6vDGYEjtZY/viewform?c=0&w=1');
});
function processData(data)
{
// Get timestamp
var now = new Date();
// Set time zone
now.setHours(now.getHours() - 6);
var timestamp = now.toUTCString();
// Parse request
var info = timestamp + ";_";
// Sample communities and areas
if(data.includes("RB"))
{
info += 'RB;_';
// Sample area
if(data.includes("North"))
info += 'North Coms;_';
else if(data.includes("Main"))
info += 'Main Coms;_';
else if(data.includes("Atrium"))
info += 'Atrium;_';
}
else if(data.includes("MU"))
{
info += 'MU;_';
// Sample area
if(data.includes("Strickland"))
info += 'Strick;_';
else if(data.includes("EllisLib"))
info += 'EllisLib;_';
else if(data.includes("Engineering"))
info += 'Engineer;_';
}
else if(data.includes("DT"))
{
info += 'DT;_';
// Sample area
if(data.includes("North"))
info += 'North;_';
else if(data.includes("West"))
info += 'West;_';
else if(data.includes("East"))
info += 'East;_';
else if(data.includes("South"))
info += 'South;_';
}
// Oh no, something's wrong
else
{
info += 'Invalid Access;_';
}
// Color
if(data.includes("Red"))
info += 'Red;_';
else if(data.includes("Blue"))
info += 'Blue;_';
else if(data.includes("Yellow"))
info += 'Yellow;_';
else if(data.includes("Blank"))
info += 'Blank;_';
// Phrase?
if(data.includes("Phrase"))
info += 'Phrase;';
else if(data.includes("NoPhrase"))
info += 'NoPhrase;';
// Terminate line
info += '__\t';
// Log
console.log('Got: ' + info);
// Log into file
fs.appendFileSync("data.txt", info, 'utf8');
}
http.listen(serverPort, serverIp,
function()
{
console.log("Listening on: " + serverIp + ":" + serverPort);
});
谢谢!
编辑:大家好,感谢大家的回答和建议。不幸的是,问题仍然存在(至少对我而言),并且由于时间限制,我不得不寻找不同的托管服务。不过,我仍然会检查这个问题,看看我们是否能解决这个问题!再次感谢!
您似乎在尝试使用 express
和 http
服务器,而且有点混用。此外,您希望 String 对象具有 includes
函数并存储您的 data.txt
,因此它不会在 git 推送 OpenShift Online 时存活下来。我对您的 server.js
代码做了一些小的修改:
使用快递:
var express = require('express');
var app = express();
var fs = require('fs');
原型 includes
方法:
String.prototype.includes = function(substr) {
return this.indexOf(substr) != -1;
}
听快递:
app.listen(serverPort, serverIp,
您最有可能还想将 data.txt
放在 OPENSHIFT_DATA_DIR
中,以免每次 git 推送时都将其擦除,但我做到了不在这里做任何改变。在 OpenShift 上查看 persistent data storage 的更多详细信息。
所以现在我有下面的东西,它似乎在 OpenShift 的 nodejs 盒式磁带上工作(我的意思是它记录东西,进行重定向并在 ~/app-root/runtime/repo/
中存储 data.txt
):
var express = require('express');
var app = express();
var fs = require('fs');
// Port which we listen to - get from OpenShift
var serverPort = process.env.OPENSHIFT_NODEJS_PORT || 80;
var serverIp = process.env.OPENSHIFT_NODEJS_IP || '127.0.0.1';
// Data logging
app.get('/:val', function(req, res)
{
// Grab request url
var data = req.url;
// Process and write to file
processData(data);
// Redirect to follow up form
res.redirect('http://goo.gl/forms/VGU9K99735');
});
function processData(data)
{
// Get timestamp
var now = new Date();
// Set time zone
now.setHours(now.getHours() - 6);
var timestamp = now.toUTCString();
// Parse request
var info = timestamp + ";_";
// seems like you want "includes" method for string objects
String.prototype.includes = function(substr)
{
return this.indexOf(substr) != -1;
}
// Sample communities and areas
if(data.includes("RB"))
{
info += 'RB;_';
// Sample area
if(data.includes("North"))
info += 'North Coms;_';
else if(data.includes("Main"))
info += 'Main Coms;_';
else if(data.includes("Atrium"))
info += 'Atrium;_';
}
else if(data.includes("MU"))
{
info += 'MU;_';
// Sample area
if(data.includes("Strickland"))
info += 'Strick;_';
else if(data.includes("EllisLib"))
info += 'EllisLib;_';
else if(data.includes("Engineering"))
info += 'Engineer;_';
}
else if(data.includes("DT"))
{
info += 'DT;_';
// Sample area
if(data.includes("North"))
info += 'North;_';
else if(data.includes("West"))
info += 'West;_';
else if(data.includes("East"))
info += 'East;_';
else if(data.includes("South"))
info += 'South;_';
}
// Oh no, something's wrong
else
{
info += 'Invalid Access;_';
}
// Color
if(data.includes("Red"))
info += 'Red;_';
else if(data.includes("Blue"))
info += 'Blue;_';
else if(data.includes("Yellow"))
info += 'Yellow;_';
else if(data.includes("Blank"))
info += 'Blank;_';
// Phrase?
if(data.includes("Phrase"))
info += 'Phrase;';
else if(data.includes("NoPhrase"))
info += 'NoPhrase;';
// Terminate line
info += '__\t';
// Log
console.log('Got: ' + info);
// Log into file
fs.appendFileSync("data.txt", info, 'utf8');
}
app.listen(serverPort, serverIp,
function()
{
console.log("Listening on: " + serverIp + ":" + serverPort);
});
//EOF
我正在尝试为学校项目创建一个 NodeJS 应用程序。基本思想是一个 NodeJS 服务器,它将处理对它的请求并跟踪具有特定格式的请求(这些将来自张贴在各个区域的二维码)。
我想使用 OpenShift 来托管应用程序,并且我可以正常构建它,但是每次我收到 URL 发布时它都会给我一个 503 错误,即使我使用了正确格式的请求(例如:psychexpserver-psychexp.rhcloud.com/MU-EllisLib-Blue-NoPhrase)。我已经在本地测试了该应用程序,它运行良好。我研究了这个主题,但没有找到适用于我的端口配置错误的答案吗?我应该去另一个 URL 吗?
代码:
var app = require('express')();
var http = require('http').Server(app);
var fs = require('fs');
// Port which we listen to - get from OpenShift
var serverPort = process.env.OPENSHIFT_NODEJS_PORT || 80;
var serverIp = process.env.OPENSHIFT_NODEJS_IP || '127.0.0.1';
app.set('port', serverPort);
app.set('ipaddr', serverIp);
// Data logging
app.get('/:val', function(req, res)
{
// Grab request url
var data = req.url;
// Process and write to file
processData(data);
// Redirect to follow up form
res.redirect('https://docs.google.com/forms/d/11JbF4FE_Vd1Dd6PD8wMbs4Kxc15GgA3NL6vDGYEjtZY/viewform?c=0&w=1');
});
function processData(data)
{
// Get timestamp
var now = new Date();
// Set time zone
now.setHours(now.getHours() - 6);
var timestamp = now.toUTCString();
// Parse request
var info = timestamp + ";_";
// Sample communities and areas
if(data.includes("RB"))
{
info += 'RB;_';
// Sample area
if(data.includes("North"))
info += 'North Coms;_';
else if(data.includes("Main"))
info += 'Main Coms;_';
else if(data.includes("Atrium"))
info += 'Atrium;_';
}
else if(data.includes("MU"))
{
info += 'MU;_';
// Sample area
if(data.includes("Strickland"))
info += 'Strick;_';
else if(data.includes("EllisLib"))
info += 'EllisLib;_';
else if(data.includes("Engineering"))
info += 'Engineer;_';
}
else if(data.includes("DT"))
{
info += 'DT;_';
// Sample area
if(data.includes("North"))
info += 'North;_';
else if(data.includes("West"))
info += 'West;_';
else if(data.includes("East"))
info += 'East;_';
else if(data.includes("South"))
info += 'South;_';
}
// Oh no, something's wrong
else
{
info += 'Invalid Access;_';
}
// Color
if(data.includes("Red"))
info += 'Red;_';
else if(data.includes("Blue"))
info += 'Blue;_';
else if(data.includes("Yellow"))
info += 'Yellow;_';
else if(data.includes("Blank"))
info += 'Blank;_';
// Phrase?
if(data.includes("Phrase"))
info += 'Phrase;';
else if(data.includes("NoPhrase"))
info += 'NoPhrase;';
// Terminate line
info += '__\t';
// Log
console.log('Got: ' + info);
// Log into file
fs.appendFileSync("data.txt", info, 'utf8');
}
http.listen(serverPort, serverIp,
function()
{
console.log("Listening on: " + serverIp + ":" + serverPort);
});
谢谢!
编辑:大家好,感谢大家的回答和建议。不幸的是,问题仍然存在(至少对我而言),并且由于时间限制,我不得不寻找不同的托管服务。不过,我仍然会检查这个问题,看看我们是否能解决这个问题!再次感谢!
您似乎在尝试使用 express
和 http
服务器,而且有点混用。此外,您希望 String 对象具有 includes
函数并存储您的 data.txt
,因此它不会在 git 推送 OpenShift Online 时存活下来。我对您的 server.js
代码做了一些小的修改:
使用快递:
var express = require('express'); var app = express(); var fs = require('fs');
原型
includes
方法:String.prototype.includes = function(substr) { return this.indexOf(substr) != -1; }
听快递:
app.listen(serverPort, serverIp,
您最有可能还想将
data.txt
放在OPENSHIFT_DATA_DIR
中,以免每次 git 推送时都将其擦除,但我做到了不在这里做任何改变。在 OpenShift 上查看 persistent data storage 的更多详细信息。
所以现在我有下面的东西,它似乎在 OpenShift 的 nodejs 盒式磁带上工作(我的意思是它记录东西,进行重定向并在 ~/app-root/runtime/repo/
中存储 data.txt
):
var express = require('express');
var app = express();
var fs = require('fs');
// Port which we listen to - get from OpenShift
var serverPort = process.env.OPENSHIFT_NODEJS_PORT || 80;
var serverIp = process.env.OPENSHIFT_NODEJS_IP || '127.0.0.1';
// Data logging
app.get('/:val', function(req, res)
{
// Grab request url
var data = req.url;
// Process and write to file
processData(data);
// Redirect to follow up form
res.redirect('http://goo.gl/forms/VGU9K99735');
});
function processData(data)
{
// Get timestamp
var now = new Date();
// Set time zone
now.setHours(now.getHours() - 6);
var timestamp = now.toUTCString();
// Parse request
var info = timestamp + ";_";
// seems like you want "includes" method for string objects
String.prototype.includes = function(substr)
{
return this.indexOf(substr) != -1;
}
// Sample communities and areas
if(data.includes("RB"))
{
info += 'RB;_';
// Sample area
if(data.includes("North"))
info += 'North Coms;_';
else if(data.includes("Main"))
info += 'Main Coms;_';
else if(data.includes("Atrium"))
info += 'Atrium;_';
}
else if(data.includes("MU"))
{
info += 'MU;_';
// Sample area
if(data.includes("Strickland"))
info += 'Strick;_';
else if(data.includes("EllisLib"))
info += 'EllisLib;_';
else if(data.includes("Engineering"))
info += 'Engineer;_';
}
else if(data.includes("DT"))
{
info += 'DT;_';
// Sample area
if(data.includes("North"))
info += 'North;_';
else if(data.includes("West"))
info += 'West;_';
else if(data.includes("East"))
info += 'East;_';
else if(data.includes("South"))
info += 'South;_';
}
// Oh no, something's wrong
else
{
info += 'Invalid Access;_';
}
// Color
if(data.includes("Red"))
info += 'Red;_';
else if(data.includes("Blue"))
info += 'Blue;_';
else if(data.includes("Yellow"))
info += 'Yellow;_';
else if(data.includes("Blank"))
info += 'Blank;_';
// Phrase?
if(data.includes("Phrase"))
info += 'Phrase;';
else if(data.includes("NoPhrase"))
info += 'NoPhrase;';
// Terminate line
info += '__\t';
// Log
console.log('Got: ' + info);
// Log into file
fs.appendFileSync("data.txt", info, 'utf8');
}
app.listen(serverPort, serverIp,
function()
{
console.log("Listening on: " + serverIp + ":" + serverPort);
});
//EOF