简体   繁体   中英

tried to upload my node.js app to heroku,it says Web process failed to bind to $PORT within 60 seconds of launch, Process exited with status 137

I tried to upload my node.js app to heroku, it says error is Web process failed to bind to $PORT within 60 seconds of launch, Process exited with status 137,how do I fix this here is my app.js

require('dotenv').config();

const createError = require('http-errors');
const express = require('express');
const engine = require('ejs-mate');
const path = require('path');
//conscd t favicon = require('serve-favicon');
const cookieParser = require('cookie-parser');
const logger = require('morgan');
const passport = require('passport');
const bodyParser = require('body-parser');
const User = require('./models/user');
const session = require('express-session');
const mongoose = require('mongoose');
const methodOverride = require('method-override');
var MongoDBStore = require('connect-mongodb-session')(session);
//const seedPosts = require('./seeds');
//seedPosts();

//require routes
const index = require('./routes/index');
const posts = require('./routes/posts');

const app = express();

console.log(process.env.DATABASEURL);
//connect to database
var url = process.env.DATABASEURL || 'mongodb://localhost:27017/transfed'
mongoose.connect(url,{
    useNewUrlParser: true ,
     useUnifiedTopology: true,
     useCreateIndex: true
});

const db = mongoose.connection;
db.on('error', console.error.bind(console, 'connection error:'));
db.once('open', () => {
    console.log("we're connected!");
});

var store = new MongoDBStore({
  uri: 'process.env.DATABASEURI',
  collection: 'mySessions'
});

// Catch errors
store.on('error', function(error) {
  console.log(error);
});

i figured it might be from the bin file but i cant find the error. heres what my bin file looks like

#!/usr/bin/env node

/**
 * Module dependencies.
 */

var app = require('../app');
var debug = require('debug')('myapp: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);
}

I used express generator to generate the skeleton of my app and am new to programming, what do I have to do to fix this issue?

First, I would try without the normalizePort() function. This is what I am using and it works perfectly:

var port = process.env.PORT || 5000;

I am not sure where you found your code but I would test with a simple example from the official documentation to make sure it works well:

const express = require('express')
const path = require('path')
const PORT = process.env.PORT || 5000

express()
  .use(express.static(path.join(__dirname, 'public')))
  .set('views', path.join(__dirname, 'views'))
  .set('view engine', 'ejs')
  .listen(PORT, () => console.log(`Listening on ${ PORT }`))

Finally, I would also check the Procfile and make sure it contains the right command .

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