Deploy node.js application to the heroic successfully, but does not work

I have a very simple direct node.js application [1] I want to deploy to a hero. Although I can expand it, the page may not be accessible in the browser.

I followed advice from the Getting Started with node.js in Heroku Guide [2]. When I launch the application using node index.jslocally, I can access the application in http://localhost:8080/index.jade, however, when I try to access it on heroku's http://immonow.herokuapp.com:8080/index.jade, it would throw me an HTTP error code ERR_CONNECTION_REFUSED.

How I deployed the application:

  • git commit -am "made changes"// commit changes
  • git push origin master// click on git
  • heroku create// create a heroku application
  • git push heroku master// click on the hero
  • heroku ps:scale web=1// launch 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

+4
1

http, . , ,

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

[1].

:

/**
 * 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] : Node.js Heroku

+2

All Articles