0

We have a Node JS - Express application running on a Openshift RHC v2 and an api is being called to a third party SMS gateway server whenever a user requests for an OTP. For this we have added a simple $http call which will trigger an SMS to the mentioned number with the message.

The request looks something like this :

$http.get("http://example.com/?user=userid:password&number=9876543210&message='Hi your OTP is 18276'")
    .success(function(error, response){
        if (!error && response.statusCode == 200) {
              console.log('STATUS: ' + response.statusCode)
              console.log('HEADERS: ' + JSON.stringify(response.headers));
        } else {
              console.log('STATUS : '+ error.statusCode);
              console.log(error);
        }

     });

This works as soon as the RHC Server starts running. After a few days or sometimes a few hours we observe that this API is never called. We have added console logs before and after the requests and still the API is never called. It is very very surprising and shocking how can it not execute this request (which is working for a few days and then goes down completely).

What could be the issue here? Will re-share what we have :

console.log("Generated OTP : " + otp);   // ... Where OTP is what we have generated... Console 1
var number = 9876543210;
var message = 'Hi your OTP is : '+otp;

console.log('Number : ' + number + ', Message : ' + message);  // ... Console 2
$http.get("http://example.com/?user=userid:password&number="+number+"&message="+message)
        .success(function(error, response){
            if (!error && response.statusCode == 200) {
                  console.log('STATUS: ' + response.statusCode) ... Console 3
                  console.log('HEADERS: ' + JSON.stringify(response.headers));  // ... Console 4
            } else {
                  console.log('STATUS : '+ error.statusCode);   // ... Console 5
                  console.log(error);   // ... Console 6
            }

         });

  console.log("callback - Post - /sellers/phone/:number/:email"); // ...Console 7
  res.json({"otp":otp});

When this is down, Consoles 1, 2 and 7 can be seen but not 3, 4 or 5 and 6.

Is there any way we can debug either on the Openshift or on the Node application?

If I do a server restart using rhc app-restart <app> the other consoles will be seen and we receive the SMS / Messages.

Please help. Any additional information needed, please let us know. Thank you.

EDIT : I have also tried using request as below :

request(url, function (error, response, body) {
        if (!error && response.statusCode == 200) {
               console.log('STATUS: ' + response.statusCode);
               console.log('HEADERS: ' + JSON.stringify(response.headers));
        } else {
               console.log('STATUS : ' + error.statusCode);
               console.log(error);
        }
  });
  • `function(error, result)` doesn't mention `response` anywhere but then is used in the function, also are you using old angular version why not using the `.then` and `.error` on the promise, success is deprecated I believe if not removed. On the node side this may help https://www.youtube.com/watch?v=1cKPPQrlC4k typically apache or nginx will have an access log imagine the same is true on express/node but unsure of the location. – shaunhusain Dec 07 '17 at 07:32
  • Sorry, I edited that to 'response'. I was typing the whole message, forgot to rename that. Instead of the $http, I changed it to request(url, function(error, response){}) – Shreyas Gombi Dec 07 '17 at 13:01
  • In either case, it should go to the if or the else? – Shreyas Gombi Dec 07 '17 at 13:06
  • Check out my awnser. Error and success methods have been removed. – Red Dec 07 '17 at 13:25

1 Answers1

0

Since AngularJS v1.6 the .success() and .error() methods on promises are removed.

Instead use the .then() method. All the data for the success, errors and headers are moved to the .then(response) response data.

So:

response.config, // Contains an object of configurations, like `Method`, `params`, `URL`
response.data, // Contains the data the request returned
response.status,  // Contains the status code, Eg 200 which is success
response.statusText, // Text of the status, Eg. Ok on success or Error on error

So check whether the $http method succeeded like so:

$http.get("http://example.com/?user=userid:password&number="+number+"&message="+message)
// Use .then instead of .success
.then(function (response) {
    if(response.status === 200) { 

        // Successfull 

    } else { 

        // Error occurred 

    }
});

Just incase you wonder why they are removed. Check out this topic:

Why are angular $http success/error methods deprecated? Removed from v1.6?

Red
  • 6,599
  • 9
  • 43
  • 85