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

时间:2016-03-17 01:07:05

标签: javascript node.js openshift

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

我想使用OpenShift来托管应用程序,我让它构建好了,但每次我发布URL都会给我503错误,即使我使用格式正确的请求(例如:psychexpserver-psychexp) .rhcloud.com / MU-EllisLib - 蓝 - NoPhrase)。我已在本地测试了该应用,它运行正常。我已经研究了这个主题,但是我发现没有任何答案可以解决我的端口配置错误吗?我应该去另一个网址吗?

代码:

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

谢谢!

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

1 个答案:

答案 0 :(得分:1)

您似乎正在尝试使用expresshttp服务器,但它有点混乱。此外,您希望String对象具有includes函数并存储您的data.txt,以便它在OpenShift Online上的git push中无法生存。我对您的server.js代码进行了一些小的修改:

  1. 使用express:

    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上的persistent data storage

  5. 所以现在我有了下面的内容,它似乎正在使用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