Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Deploy node.js app on heroku succeeds but doesn't work

I have a very simple, straight-forward node.js app [1] I want to deploy on heroku. Although I'm able to deploy it, the page can't be accessed in the browser.

I followed the advices from the 'Getting Started with Node.js on Heroku' guide [2]. When I run the application using node index.js locally, I'm able to access the application at http://localhost:8080/index.jade, however, when I try to access it on heroku at http://immonow.herokuapp.com:8080/index.jade it would throw me a ERR_CONNECTION_REFUSED HTTP error code.

How I deployed my app:

  1. git commit -am "made changes" // commit changes
  2. git push origin master // push to git
  3. heroku create // create heroku app
  4. git push heroku master // push to heroku
  5. heroku ps:scale web=1 // start workers

My node.js server:

#!/usr/bin/env node
var http = require('http')
  , jade = require('jade')
  , static = require('node-static')
  , jadeRe = /\.jade$/
  , path = process.argv.slice(2)[0]
  , fileServer = new static.Server(path || '.')

http.createServer(function (req, res) {
  if (req.url.match(jadeRe)) {
    res.writeHead(200, {'Content-Type': 'text/html'})
    res.end(jade.renderFile('.' + req.url, {
      filename: '.' + req.url.replace(jadeRe, '')
    }))
  } else {
    req.addListener('end', function () {
      fileServer.serve(req, res)
    }).resume()
  }
}).listen(8080)

Any help would be appreciated.

[1] https://github.com/takahser/immonow

[2] https://devcenter.heroku.com/articles/getting-started-with-nodejs#introduction

like image 440
Ronin Avatar asked Jun 11 '15 17:06

Ronin


2 Answers

Since I was not able to get it to work using the http package, I decided to use express instead. As for the port, I had to do it as follow

var port = process.env.PORT || 3000;
app.listen(port);

in order to get it to work [1].

Here is my full working server:

/**
 * Module dependencies.
 */
var express = require('express');

// Path to our public directory
var pub = __dirname + '/public';

// setup middleware
var app = express();
app.use(express.static(pub));
app.use("/css", express.static(__dirname + '/css'));
app.use("/font", express.static(__dirname + '/font'));
app.use("/img", express.static(__dirname + '/img'));
app.use("/js", express.static(__dirname + '/js'));
app.use("/video", express.static(__dirname + '/video'));

// Set our default template engine to "jade"
// which prevents the need for extensions
// (although you can still mix and match)
app.set('view engine', 'jade');

app.get('/', function(req, res){
  res.render('index');
});

app.get('/*', function(req, res){
  console.log(req.url.replace("/",""));
  res.render(req.url.replace("/",""));
});

// change this to a better error handler in your code
// sending stacktrace to users in production is not good
app.use(function(err, req, res, next) {
  res.send(err.stack);
});

/* istanbul ignore next */
if (!module.parent) {
  var port = process.env.PORT || 3000;
  app.listen(port);
  console.log('Express started on port 3000');
}

[1] see: Node.js port issue on Heroku cedar stack

like image 125
Ronin Avatar answered Sep 28 '22 00:09

Ronin


Things I had to do..

  1. Create a Procfile in the root directory (a file literally called Procfile, no extension).
    Inside Procfile, type:

    web: node server.js

  2. Add a script and engine to package.json

    "scripts": { "start": "node server.js" }

    "engines": { "node": "8.9.3" }

  3. Update port in server.js

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

And why..

  1. To explicitly declare what command should be executed to start your app. Heroku looks for a Procfile specifying your process types

  2. For the script, if no Procfile is present in the root directory during the build process, your web process will be started by running npm start. For the engine, to specify a Node version that matches the runtime you’re developing with and want to use on Heroku.

  3. Heroku already assigns your app a port and adds it to the env, so you can't set the port to a fixed number.

Resources:

https://devcenter.heroku.com/articles/getting-started-with-nodejs#introduction

https://devcenter.heroku.com/articles/troubleshooting-node-deploys

https://devcenter.heroku.com/articles/deploying-nodejs

like image 37
Chareesa Graham Avatar answered Sep 28 '22 00:09

Chareesa Graham