Saturday, 13 May 2017

Unable to ping heroku

I have a nodejs app running on Heroku. Here is the server.js file

var express = require('express')
  , cors = require('cors')
  , app = express();
var http = require('http').Server(app);
var io = require("socket.io").listen(http);
app.use(cors());
require('./modules/routes.js')(app,io);
app.set('port', process.env.PORT || 5000);

app.listen(app.get('port'), function () {
    console.log('Express server listening on port ' + app.get('port'));
});

Here is my routes.js

"use strict";
const bodyParser = require('body-parser');
var cookieParser = require('cookie-parser');
var session = require('express-session');
var time = require('express-timestamp');
var Promise = require('promise');
var momentjs = require('moment');
var _ = require('lodash');

var method = routes.prototype;

function routes(app, io) {

    app.use(time.init);
    app.use(cookieParser());

    app.use(session({ secret: 'asdo8rter65edfgfd53wet34634632y4bluaq', resave: true, saveUninitialized: true }));

    app.all('/', function(req, res, next) {
        res.header("Access-Control-Allow-Origin", "*");
        res.header("Access-Control-Allow-Headers", "X-Requested-With");
        next();
    });
    app.use(bodyParser.json());
    app.post('/testHeroku', function(req, res) {
        console.log(req);
        res.write(JSON.stringify({
            process: "success"
        }));
        res.end();
    });
}
method.getroutes = function() {
    return this;
}

module.exports = routes;

I'm trying to access /testHeroku from an ionic app running in android emulator.

Ionic code:

vm.testHeroku = function(){
      console.log('testing heroku');
            var testdata = {
              url: config.baseURL + 'testHeroku',
              dataServer: {
                serverTaskRequest: 'getUADSF'
              }
            }
            runajax.runajax_function(testdata, function (testdataResponse) {
              if (testdataResponse.process == 'success') {
                alert(testdataResponse.process);
              }
            }); 
    };

Here goes my config.baseURL = abcd-1234.herokuapp.com (This is example for the heroku app url) I don't receive any return form the http call.

Code for run_ajax service

.service('runajax', ['$http', function ($http) {
    this.runajax_function = function (request, callback) {
      var url = request.url;
      var dataServer = request.dataServer;
      // console.log('runajax function called -> ' + url);
      // console.log(dataServer);
      $http.post(url, dataServer).success(function (data, status, headers, config) {
          callback(data);
        })
        .error(function () {
          callback(status);
        });
    }
  }])



via Subrata Banerjee

No comments:

Post a Comment