Node.js API working locally but not on Heroku

安稳与你 提交于 2021-01-24 10:56:19

问题


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

回答1:


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!




回答2:


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.



来源:https://stackoverflow.com/questions/42732525/node-js-api-working-locally-but-not-on-heroku

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!