简体   繁体   中英

Deploying a Node.js(+MongoDB) app with Heroku, always Local Port

I've searched and tried a lot of things, and I can't get my deployment to work. My node.js app works locally, but doesn't work when deployed on heroku. The problem is the port I'm using, I believe.

In my app.js,I have:

1) a server+port:

var server = require('http').createServer();
var port = process.env.PORT || 5000;

2) The port I want to listen to: (I do think this part is wrong)

server.listen(port, function() { 
  console.log("Listening on  " + port);
});

app.listen(port);
// app.listen(process.env.PORT || 5000);

module.exports = app;

3) And some other stuff

var app = express();
app.use(express.static(path.join(__dirname, 'public')));
// view engine setup
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'jade');

 // uncomment after placing your favicon in /public
//app.use(favicon(path.join(__dirname, 'public', 'favicon.ico')));
app.use(logger('dev'));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));
app.use(cookieParser());

//app.use(express.static(__dirname + '/'))


// var routes = require('./routes/index');
var routes = require('./routes/index')(passport);
var users = require('./routes/users');


// make db accessible to the router
app.use(function(req,res,next){
   req.db = db;
   next();
});

app.use('/', routes);
app.use('/users', users);
// app.use('/api', api);



// catch 404 and forward to error handler
app.use(function(req, res, next) {
   var err = new Error('Not Found');
   err.status = 404;
   next(err);
});

// error handlers

//development error handler
//will print stacktrace
if (app.get('env') === 'development') {
   app.use(function(err, req, res, next) {
      res.status(err.status || 500);
      res.render('error', {
         message: err.message,
         error: err
      });
   });
}

// production error handler
// no stacktraces leaked to user
app.use(function(err, req, res, next) {
   res.status(err.status || 500);
   res.render('error', {
      message: err.message,
       error: {}
    });
});

When I'm on my deployed app, and here you can see it says it will redirect to the place I want (at the bottom left) the frozen-fortress.../login/facebook..etc however, when i actually click on it, it gives me localhost:3000/login/facebook...etc

This happens with some of my others links too, but NOT all the links.

What can I do the change the port? I have tried things like app.listen(port) as well.

Thank you in advance! (Also I apologize if the format of my question looks funky, this is my first time on stackoverflow).

I might be wrong, but

look at this part of code:

var server = require('http').createServer();
var port = process.env.PORT || 5000;
// ...
server.listen(port, function() { 
  console.log("Listening on  " + port);
});

app.listen(port);

You create two servers here: server and Express' app . Both will try to listen on the same port and one of them will fail. I mean, Express already has http server inside, you don't need another one:

var port = process.env.PORT || 5000;
app = express()
app.listen(port)
app.use(...)
app.get('/', function(...){
   // there you go
})

Here's doc about listen() and other Express things

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