简体   繁体   中英

Code breaks on Heroku, but works on localhost

I have this simple code:

router.get('/generateTime', cors(), (req, res, next) => {
    var now = new Date();
    res.json({
        "date": now.getDate(),
        "day": now.getDay(),
        "hours": now.getHours(),
        "minutes": now.getMinutes(),
        "month": now.getMonth(),
        "seconds": now.getSeconds(),
        "time": now.getTime(),
        "timezoneOffset": now.getTimezoneOffset(),
        "year": now.getFullYear() - 1900,
    });
});

This works great on localhost and results in:

{"date":12,"day":3,"hours":17,"minutes":45,"month":8,"seconds":29,"time":1536759929173,"timezoneOffset":-120,"year":118}

But when I use it on Heroku I get application Error. The logs state:

2018-09-12T13:50:25.541953+00:00 heroku[web.1]: State changed from starting to crashed

2018-09-12T13:50:32.696713+00:00 heroku[router]: at=error code=H10 desc="App crashed" method=GET path="/" host=uczich-helper.herokuapp.com request_id=02574304-0a4f-413c-8da5-316a07805d81 fwd="109.196.117.12" dyno= connect= service= status=503 bytes= protocol=https

2018-09-12T13:50:33.926344+00:00 heroku[router]: at=error code=H10 desc="App crashed" method=GET path="/" host=uczich-helper.herokuapp.com request_id=965dc224-3922-400e-9af3-f7b648dc49a3 fwd="109.196.117.12" dyno= connect= service= status=503 bytes= protocol=https

I am using the free dynos. I tried deleting the application and creating a new one, but no luck.

EDIT:

Here's the www file:

#!/usr/
bin / env
node

/**
 * Module dependencies.
 */

var app = require('../app');
var debug = require('debug')('mailer:server');
var http = require('http');

/**
 * Get port from environment and store in Express.
 */

var port = normalizePort(process.env.PORT || '4000');
app.set('port', port);

/**
 * Create HTTP server.
 */

var server = http.createServer(app);

/**
 * Listen on provided port, on all network interfaces.
 */

server.listen(port);
server.on('error', onError);
server.on('listening', onListening);

/**
 * Normalize a port into a number, string, or false.
 */

function normalizePort(val) {
    var port = parseInt(val, 10);

    if (isNaN(port)) {
        // named pipe
        return val;
    }

    if (port >= 0) {
        // port number
        return port;
    }

    return false;
}

/**
 * Event listener for HTTP server "error" event.
 */

function onError(error) {
    if (error.syscall !== 'listen') {
        throw error;
    }

    var bind = typeof port === 'string'
        ? 'Pipe ' + port
        : 'Port ' + port;

    // handle specific listen errors with friendly messages
    switch (error.code) {
        case 'EACCES':
            console.error(bind + ' requires elevated privileges');
            process.exit(1);
            break;
        case 'EADDRINUSE':
            console.error(bind + ' is already in use');
            process.exit(1);
            break;
        default:
            throw error;
    }
}

/**
 * Event listener for HTTP server "listening" event.
 */

function onListening() {
    var addr = server.address();
    var bind = typeof addr === 'string'
        ? 'pipe ' + addr
        : 'port ' + addr.port;
    debug('Listening on ' + bind);
}

Here's my package.json file:

{
  "name": "mailer",
  "version": "0.0.0",
  "private": true,
  "scripts": {
    "start": "node ./bin/www"
  },
  "dependencies": {
    "body-parser": "~1.18.2",
    "cookie-parser": "~1.4.3",
    "debug": "~2.6.9",
    "ejs": "~2.5.7",
    "express": "~4.15.5",
    "morgan": "~1.9.0",
    "serve-favicon": "~2.4.5"
  },
  "devDependencies": {
    "cors": "^2.8.4",
    "nodemailer": "^4.6.7"
  }
}

Turns out I had to move dependencies from devDependencies to dependencies . No idea why npm did this now.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM