Heroku + node.js error (Web process failed to bind to $PORT within 60 seconds of launch)

前端 未结 24 2505
梦如初夏
梦如初夏 2020-11-22 05:42

I have my first node.js app (runs fine locally) - but I am unable to deploy it via heroku (first time w/ heroku as well). The code is below. SO doesn\'t let me write so much

24条回答
  •  夕颜
    夕颜 (楼主)
    2020-11-22 06:29

    In my case, neither the port nor the host was the problem. The index.js was divided into 2 files. server.js:

    //server.js
    const express = require('express')
    const path = require('path')
    
    const app = express()
    
    app.use(express.static(path.resolve(__dirname, 'public')));
    // and all the other stuff
    module.exports = app
    
    //app.js
    const app = require('./server');
    const port = process.env.PORT || 3000;
    app.listen(port, '0.0.0.0', () => {
        console.log('Server is running s on port: ' + port)
    });
    

    from package.json we ran node app.js.

    Apparently that was the problem. Once I combined the two into one file, the Heroku app deployed as expected.

提交回复
热议问题