Jump to content
Search In
  • More options...
Find results that contain...
Find results in...
DarkSwordsman

Node/Express; http.get not working

Recommended Posts

Posted · Original PosterOP

Hey All,

I launched an app on Heroku (http://ugate.me) (which I am currently building out at the moment) and I am running it on a free server, which really isn't that bad. The only issue is that it sleeps after 30 minutes of inactivity. 

When it is asleep, it takes about 9 seconds for it to start back up, but for someone that is coming to the website, I'm sure they would find that very annoying. I tried doing this, but I have found no avail. I did import the http package but it doesn't seem to do anything. Also, app.get() didn't seem to be working with normal connections. I am using express on node.

 

var http = require('http');
// time in seconds since last connection
var lastConnection = 0;
// if someone connects, reset the timer
app.get('/', function() {
  lastConnection = 0;
});
// add time; maybe ping
setInterval(function() {
  lastConnection++;
  // if the connection is 15 minutes old, ping the server to keep it alive
  if(lastConnection >= 900){
    http.get("http://ugate.me");
    console.log("Keep Alive - Heartbeat");
    lastConnection = 0; // reset the timer since the heartbeat was sent out
  }
}, 1000);

Anyone have any ideas?

- Kyle

Link to post
Share on other sites

What exactly is your problem with http and express? The idle mode after 30 minutes is a limitation by heroku to save server resources for paying customers.

I guess the reason why express is not behaving as expected is that you don't require express and define the app variable you seem to be using. But your source looks pretty incomplete, so I don't know if this is actually a complete file. 


"I speak English very well, bröcklersweiß und nier zu schnell"

-- Probably the favorite quote from bavarian students

Codepen http://codepen.io/MoVo99/

Software Skills

  • HTML, CSS & Client Side JavaScript
  • PHP (Laravel & Lumen)
  • JavaScript (NodeJs with Express)
  • Java (Android SDK; Bukkit SDK)
  • Ruby On Rails (Just learning.... 20%)
Link to post
Share on other sites
Posted · Original PosterOP
10 hours ago, MoVo said:

What exactly is your problem with http and express? The idle mode after 30 minutes is a limitation by heroku to save server resources for paying customers.

I guess the reason why express is not behaving as expected is that you don't require express and define the app variable you seem to be using. But your source looks pretty incomplete, so I don't know if this is actually a complete file. 

 

My bad, I didn't include the whole source file. The main issues were that it seemed like the setInterval for the http.get request wasn't working, and then the app.get wasn't working as well. I will try to come back to this tomorrow and sort it out, but the main point in it is to keep the server alive so people don't need to wait 9 seconds and think the website is toast (I'm just finishing my freshman year at college, so free is music to my ears, and I'd like to keep it that way, lol).
 

// import everything that we need
var express = require('express');
var app = express();
var mongoose = require('mongoose');
var http = require('http');
// environment config
var config = require('./config')(process.env.NODE_ENV);
var appPort = process.env.PORT || config.port;
// mongodb connect
var connect = require('./connect.js');


// export the app
module.exports = function(){
  // connect to mongodb
  connect();

  // express
  app.use('/', express.static('./client/pages'));
  // use the body parser (for build version)
  // app.use(bodyParser.json());

  app.listen(appPort, function(){
    console.log('GATE app listening on port ' + appPort);
    console.log('Running Environment: ' + config.name);
  });
  // time in seconds since last connection
  var lastConnection = 0;
  // add time; maybe ping
  setInterval(function() {
    lastConnection++;
    // if the connection is 15 minutes old, ping the server
    if(lastConnection >= 900){
      http.get("http://ugate.me");
      console.log("Keep Alive - Heartbeat");
      lastConnection = 0;
    }
  }, 1000);
}

 

Link to post
Share on other sites

If you are defining the app like this you'd have to require this file and run the function you are exporting. Otherwise, the function you are exporting isn't being called. Simple move to function code into the main file and remove the module export. If you want to use the app instance for testing with mocha etc. you can just do 

module.exports = app;

which should work fine for the most scenarios. 


"I speak English very well, bröcklersweiß und nier zu schnell"

-- Probably the favorite quote from bavarian students

Codepen http://codepen.io/MoVo99/

Software Skills

  • HTML, CSS & Client Side JavaScript
  • PHP (Laravel & Lumen)
  • JavaScript (NodeJs with Express)
  • Java (Android SDK; Bukkit SDK)
  • Ruby On Rails (Just learning.... 20%)
Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now


×