2

I'm trying to create a node.js application with Heroku. In my server.js file I use express to load a static html page. This is the express code in my server.js file. In the procfile from Heroku I set this as my starting point.

var app = require('express')();

var http = require('http').Server(app);

app.get('/', function(req, res){
  res.sendFile(__dirname + 'index.html');
});

But when I push it to Heroku, Heroku doesn't show anything, so also no errors.

server.js, index.html are in the same folder

1 Answers1

1

Is that your entire server.js? If so, you need to specify a port to be listened to:

var app = require('express')();
var server = require('http').Server(app);
server.listen(80);

app.get('/', function (req, res) {
  res.sendfile(__dirname + '/index.html');
});
console.log('Express server started on port %s', server.address().port);

EDIT: As @Abdelouahab suggested, you shouldn't hardcode the port, rather grab it from process.env.PORT

var port = process.env.PORT || 3000;
app.get('/', function (req, res) {
  res.sendfile(__dirname + '/index.html');
});
app.listen(port, function() {
  console.log("Node app is running at localhost:" + app.get('port'))
});

https://devcenter.heroku.com/articles/getting-started-with-nodejs#push-local-changes

Node.js port issue on Heroku cedar stack

Community
  • 1
  • 1
trekforever
  • 3,914
  • 25
  • 29
  • in Heroku it is adviced to not hard code the port `app.set('port', process.env.PORT || 80)` `var server = app.listen(app.get('port'), function () { console.log('working! on port ' + app.get('port'))` }) – Abdelouahab Nov 22 '14 at 00:21