0

I create a node app that is working locally. I need help to modify server.js below so that it can work on heroku.

server.js:

var http = require('http');
var session = require('cookie-session');
var port = 9500; 
var Assets = require('./backend/Assets'); 
var API = require('./backend/API'); 

var Default = require('./backend/Default');
var Chat = require('./backend/Chat');  
var Router = require('./frontend/js/lib/router')();  

Router 
.add('static', Assets) 
.add('node_modules', Assets) 
.add('tests', Assets) 
.add('api', API) 
.add(Default);  

var checkSession = function(req, res) {
session({     
  keys: ['nodejs-by-example']
})(req, res, function() { 
  process(req, res);  
}); 
}  

var process = function(req, res) { 
  Router.check(req.url, [req, res]); 
}  

var app = http.createServer(checkSession).listen(port, '127.0.0.1'); 
console.log("Listening on 127.0.0.1:" + port);  
Chat(app); 

I tried:

var port = Number(process.env.PORT || 9000); 

But it didn't work. I got this error:

users-MacBook-Pro-6:project user$ node server
/Users/user/Desktop/project/server.js:3
var port = Number(process.env.PORT || 5000); 
TypeError: Cannot read property 'env' of undefined
    at Object.<anonymous> (/Users/user/Desktop/project/server.js:3:26)
    at Module._compile (module.js:456:26)
    at Object.Module._extensions..js (module.js:474:10)
    at Module.load (module.js:356:32)
    at Function.Module._load (module.js:312:12)
    at Function.Module.runMain (module.js:497:10)
    at startup (node.js:119:16)
    at node.js:906:3
cchamberlain
  • 17,444
  • 7
  • 59
  • 72

1 Answers1

0

It looks like you are getting an undefined process variable. To troubleshoot you should do a console.dir(process) which will print out everything on the process variable including your environment variables.

As well you can try doing this - heroku config:set NODE_ENV=production per heroku support

Could possibly be related to this.

Update

To further troubleshoot you will need to install a logging addon-

heroku addons:create papertrail

After you install you will be able to navigate to the add-on and see any messages that are being logged related to the deployment as well as your console.dir and console.log printouts.

Community
  • 1
  • 1
cchamberlain
  • 17,444
  • 7
  • 59
  • 72