2

For some crazy reason my local Node server is working when I test with postman but when I upload to Heroku I get the following error:

at=error code=H12 desc="Request timeout" method=POST path="/api/messages" host=test.herokuapp.com request_id=big_number_here fwd="my_ip_address" dyno=web.1 connect=0ms service=30000ms status=503 bytes=0 protocol=https

It just seems to time out. I tested another route, just a normal front end route, and it loads correctly. My code is below:

app.js

var routesApi = require('./app_api/routes/index');
app.use('/api', routesApi);

app_api/routes

var express = require('express');
var router = express.Router();

var ctrlMessages = require('../controllers/messages');

// Messages
router.post('/messages', ctrlMessages.messagesCreate);


module.exports = router;

EDIT

messages.js (adding controller code)

var mongoose = require('mongoose');
var Msg = mongoose.model('Messages');

// mongoose.set('debug', true); // TESTING

var sendJsonResponse = function(res, status, content) {
    res.status(status);
    res.json(content);
};

/* POST a new location */
/* /api/messages */
module.exports.messagesCreate = function(req, res) {
    Msg.create({
        msg1: req.body.msg1,
        msg2: req.body.msg2
    }, function(err, msg) {
        if (err) {
            console.log(err);
            sendJsonResponse(res, 400, err);
        } else {
            console.log(err);
            sendJsonResponse(res, 201, msg);
        }
    });
};

Model messages.js

var mongoose = require('mongoose');

var msgdata = new mongoose.Schema({
    msg1: {
        type: String,
        required: true,
    },
    msg2: {
        type: String,
        required: true
    },
    createdOn: {
        type: Date,
        "default": Date.now
    }
});

mongoose.model('Messages', msgdata);

DB Connector db.js

var mongoose = require('mongoose');
var gracefulShutdown;
var dbURI = 'mongodb://locationToMyMongoDB';


if (process.env.NODE_ENV === 'production') {
    dbURI = process.env.MONGOLAB_URI;
}
mongoose.connect(dbURI);

// Emulate SIGINT signal for Windows
var readLine = require('readline');
if (process.platform === "win32") {
    var rl = readLine.createInterface ({
        input: process.stdin,
        output: process.stdout
    });
    rl.on ('SIGINT', function() {
        process.emit ("SIGINT");
    });
}

mongoose.connection.on('connected', function() {
    console.log('Mongoose connected to ' + dbURI);
});
mongoose.connection.on('error', function(err) {
    console.log('mongoose connection error: ' + err);
});
mongoose.connection.on('disconnected', function() {
    console.log('Mongoose disconnected');
});

gracefulShutdown = function(msg, callback) {
    mongoose.connection.close(function() {
        console.log('Mongoose disconnected through ' + msg);
        callback();
    });
};

// For nodemon restarts
process.once('SIGUSR2', function() {
    gracefulShutdown('nodemon restart', function() {
        process.kill(process.pid, 'SIGUSR2');
    });
});
// For app termination
process.on('SIGINT', function() {
    gracefulShutdown('app termination', function() {
        process.exit(0);
    });
});
// For Heroku app termination
process.on('SIGTERM', function() {
    gracefulShutdown('Heroku app shutdown', function() {
        process.exit(0);
    });
});

// BRING IN SCHEMAS & MODELS
require('./messages');
4
  • 1
    Can you post source code of ctrlMessages? Commented Mar 11, 2017 at 9:41
  • Hey, I added the controller code at the bottom. Commented Mar 11, 2017 at 16:14
  • Have you tried sticking in some logging messages to see if any of the code is executing? Commented Mar 11, 2017 at 16:41
  • @toddg no but that's a good idea, I should do that. Commented Mar 11, 2017 at 16:46

2 Answers 2

2

So this is kind of funny, but such a simple solution:

excerpt from db.js

...
var dbURI = 'mongodb://locationToMyMongoDB';

if (process.env.NODE_ENV === 'production') {
    dbURI = process.env.MONGOLAB_URI;
}
...

I was using a hard coded dbURI variable and not listed it as an environmental variable, therefore my code was checking to see if process.env.MONGOLAB_URI existed, and it didn't.

I simply commented that line out and it worked perfectly!

Sign up to request clarification or add additional context in comments.

Comments

0

Are there a lot of messages on the production/Heroku version of the API?

An H12 error code means that the request has timed out (see https://devcenter.heroku.com/articles/error-codes#h12-request-timeout), as Heroku enforces a max limit of 30 seconds.

Try paginating your endpoint, or returning less data.

1 Comment

Hey, thanks for the answer. There's honestly not much data at all being passed around, like just a few words. I've updated the OP with my controller code if that helps.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.