OpenShift NodeJS 应用程序 503 错误(端口问题?

OpenShift NodeJS Application 503 Error (port issue?)

本文关键字:问题 错误 NodeJS 应用程序 OpenShift      更新时间:2023-09-26

我正在尝试为学校项目创建一个 NodeJS 应用程序。基本思想是一个 NodeJS 服务器,它将处理对它的请求并跟踪特定格式的请求(这些将来自各个区域发布的二维码)。

我想使用 OpenShift 来托管应用程序,并且我让它构建正常,但是每次我发布 URL 时,即使我使用格式正确的请求(例如:psychexpserver-psychexp.rhcloud.com/MU-EllisLib-Blue-NoPhrase),它也会给我一个 503 错误。我已经在本地测试了该应用程序,它工作正常。我已经研究了这个主题,但我没有找到适合我的答案 端口配置错误吗?我应该转到其他网址吗?

法典:

    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);
                   });

谢谢!

编辑:嘿伙计们,感谢大家的回答和建议。不幸的是,问题仍然存在(至少对我来说),由于时间限制,我不得不寻找不同的托管服务。不过,我仍然会检查这个问题,看看我们是否可以解决这个问题!再次感谢!

您似乎正在尝试使用expresshttp服务器,而且它有点混合。此外,您希望 String 对象具有includes函数并存储您的data.txt,这样它就不会在 OpenShift Online 上的 git 推送中幸存下来。我对你的server.js代码做了一些小的编辑:

  1. 使用快递:

    var express = require('express');
    var app = express();
    var fs = require('fs');
    
  2. includes方法进行原型设计:

    String.prototype.includes = function(substr) {
      return this.indexOf(substr) != -1;
    }
    
  3. 听快递:

    app.listen(serverPort, serverIp,
    
  4. 您还希望最有可能将data.txt放在OPENSHIFT_DATA_DIR中,以免每次推送 git 时都擦除它,但我在这里没有做任何更改。查看有关 OpenShift 上的持久数据存储的更多详细信息。

所以现在我有以下内容,它似乎正在 OpenShift 的 nodejs 墨盒上工作(我的意思是它会记录内容、进行重定向并将data.txt存储在 ~/app-root/runtime/repo/ 中):

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