代码之家  ›  专栏  ›  技术社区  ›  Sangram Badi

Nodejs项目无法在heroku中启动

  •  1
  • Sangram Badi  · 技术社区  · 6 年前

    我正在尝试在heroku.com中部署nodejs应用程序。我成功地把代码推了进去 heroku master . 当我试图访问此应用程序时,它显示以下错误。

    enter image description here

    我也检查日志和下面的错误我得到。

    2018-07-25T06:17:36.798787+00:00 heroku[web.1]: Starting process with command `npm start`
    2018-07-25T06:17:39.066859+00:00 app[web.1]:
    2018-07-25T06:17:39.066885+00:00 app[web.1]: > nodegigapi@0.0.0 start /app
    2018-07-25T06:17:39.066887+00:00 app[web.1]: > node ./bin/www
    2018-07-25T06:17:39.066888+00:00 app[web.1]:
    2018-07-25T06:17:39.138914+00:00 app[web.1]: module.js:549
    2018-07-25T06:17:39.138917+00:00 app[web.1]: throw err;
    2018-07-25T06:17:39.138918+00:00 app[web.1]: ^
    2018-07-25T06:17:39.138920+00:00 app[web.1]:
    2018-07-25T06:17:39.138921+00:00 app[web.1]: Error: Cannot find module '/app/bin/www'
    2018-07-25T06:17:39.138923+00:00 app[web.1]: at Function.Module._resolveFilename (module.js:547:15)
    2018-07-25T06:17:39.138924+00:00 app[web.1]: at Function.Module._load (module.js:474:25)
    2018-07-25T06:17:39.138925+00:00 app[web.1]: at Function.Module.runMain (module.js:693:10)
    2018-07-25T06:17:39.138926+00:00 app[web.1]: at startup (bootstrap_node.js:191:16)
    2018-07-25T06:17:39.138927+00:00 app[web.1]: at bootstrap_node.js:612:3
    2018-07-25T06:17:39.144035+00:00 app[web.1]: npm ERR! code ELIFECYCLE
    2018-07-25T06:17:39.144315+00:00 app[web.1]: npm ERR! errno 1
    2018-07-25T06:17:39.145314+00:00 app[web.1]: npm ERR! nodegigapi@0.0.0 start: `node ./bin/www`
    2018-07-25T06:17:39.145428+00:00 app[web.1]: npm ERR! Exit status 1
    2018-07-25T06:17:39.145607+00:00 app[web.1]: npm ERR!
    2018-07-25T06:17:39.145751+00:00 app[web.1]: npm ERR! Failed at the nodegigapi@0.0.0 start script.
    2018-07-25T06:17:39.149954+00:00 app[web.1]:
    2018-07-25T06:17:39.145852+00:00 app[web.1]: npm ERR! This is probably not a problem with npm. There is likely additional logging output above.
    2018-07-25T06:17:39.150091+00:00 app[web.1]: npm ERR! A complete log of this run can be found in:
    2018-07-25T06:17:39.150165+00:00 app[web.1]: npm ERR!     /app/.npm/_logs/2018-07-25T06_17_39_147Z-debug.log
    2018-07-25T06:17:39.195926+00:00 heroku[web.1]: Process exited with status 1
    2018-07-25T06:17:39.212995+00:00 heroku[web.1]: State changed from starting to crashed
    2018-07-25T06:17:39.215182+00:00 heroku[web.1]: State changed from crashed to starting
    2018-07-25T06:17:41.663113+00:00 heroku[web.1]: Starting process with command `npm start`
    2018-07-25T06:17:43.846000+00:00 app[web.1]:
    2018-07-25T06:17:43.846025+00:00 app[web.1]: > node ./bin/www
    2018-07-25T06:17:43.846023+00:00 app[web.1]: > nodegigapi@0.0.0 start /app
    2018-07-25T06:17:43.846027+00:00 app[web.1]:
    2018-07-25T06:17:43.920839+00:00 app[web.1]: module.js:549
    2018-07-25T06:17:43.920842+00:00 app[web.1]: throw err;
    2018-07-25T06:17:43.920843+00:00 app[web.1]: ^
    2018-07-25T06:17:43.920847+00:00 app[web.1]: at Function.Module._resolveFilename (module.js:547:15)
    2018-07-25T06:17:43.920844+00:00 app[web.1]:
    2018-07-25T06:17:43.920845+00:00 app[web.1]: Error: Cannot find module '/app/bin/www'
    2018-07-25T06:17:43.920848+00:00 app[web.1]: at Function.Module._load (module.js:474:25)
    2018-07-25T06:17:43.920850+00:00 app[web.1]: at startup (bootstrap_node.js:191:16)
    2018-07-25T06:17:43.920849+00:00 app[web.1]: at Function.Module.runMain (module.js:693:10)
    2018-07-25T06:17:43.920852+00:00 app[web.1]: at bootstrap_node.js:612:3
    2018-07-25T06:17:43.926342+00:00 app[web.1]: npm ERR! errno 1
    2018-07-25T06:17:43.926056+00:00 app[web.1]: npm ERR! code ELIFECYCLE
    2018-07-25T06:17:43.927330+00:00 app[web.1]: npm ERR! nodegigapi@0.0.0 start: `node ./bin/www`
    2018-07-25T06:17:43.927429+00:00 app[web.1]: npm ERR! Exit status 1
    2018-07-25T06:17:43.927591+00:00 app[web.1]: npm ERR!
    2018-07-25T06:17:43.927697+00:00 app[web.1]: npm ERR! Failed at the nodegigapi@0.0.0 start script.
    2018-07-25T06:17:43.927800+00:00 app[web.1]: npm ERR! This is probably not a problem with npm. There is likely additional logging output above.
    2018-07-25T06:17:43.934451+00:00 app[web.1]: npm ERR!     /app/.npm/_logs/2018-07-25T06_17_43_929Z-debug.log
    2018-07-25T06:17:43.934236+00:00 app[web.1]:
    2018-07-25T06:17:43.934368+00:00 app[web.1]: npm ERR! A complete log of this run can be found in:
    

    下面是我的项目结构。我开始申请 npm start 命令。和 app.js 有我的服务器代码。在公共文件夹中,我有angular js 1代码。

    应用程序js

    app.use('/api', googleRouter);
    
    app.listen(2020, function () {
      console.log('Server strated on port 2020 !');
    });
    
    module.exports = app;
    

    enter image description here

    银行标识代码/www

    #!/usr/bin/env node
    
    /**
     * Module dependencies.
     */
    
    var app = require('../app');
    var debug = require('debug')('nodegigapi:server');
    var http = require('http');
    
    /**
     * Get port from environment and store in Express.
     */
    
    var port = normalizePort(process.env.PORT || '3000');
    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);
    }
    

    有人能指导我吗?

    1 回复  |  直到 6 年前
        1
  •  1
  •   Hòa Nguyễn Coder    6 年前

    您需要创建文件Procfile upload到heroku,您可以尝试以下操作或查看: How to Deploy a Node.js App to Heroku

    //Procfile
    web: node app.js
    
    //app.js
    var express =  require('express');
    var app      = express();
    var http = require('http').Server(app);
    
    app.get('/',function(req,res){
    	res.send('Hello');
    })
    
    http.listen(process.env.PORT || 8888, function(){
      console.log('listening on *:8888');
    });