0

I am new to node.js and heroku. By following docs and some tutorials I have successfully deployed my app on heroku.

The problem I am facing is that I don't know how to run that deployed app on heroku. After reading a number of tutorials, its still not clear to me.

When I run the commands

$ git add .
$ git commit -am "make it better"
$ git push heroku master

from local command line, it executes the scripts. But when I click on the open app from the heroku account, it does not execute all of files.

I am running 1 web dynos and heroku shows that my web dyno executes following command

web node build/server.js

The server.js file contains following code

require('./routes/main.routes.js');

var http = require('http'); 
http.createServer(function (req, res) {
  res.writeHead(200, {'Content-Type': 'text/plain'});
  res.end("this is a test page");
}).listen(process.env.PORT);

when I click on open app from heroku, it opens the app in the browser with message
this is a test page

It does not run the main.routes.js file.
But when I click on restart all Dynos, the main.routes.js file also gets executed. I want the app to execute the main.routes.js file when I click on open my app or refresh it in the browser window.

Is the restart all Dynos only way to execute script? or I have done something wrong?

Any help will be much appreciated.

Atif
  • 280
  • 1
  • 11
  • did you try `heroku run` in command prompt? – Dhananjaya Kuppu May 25 '16 at 11:39
  • @DhananjayaKuppu yes but it says, run is not a valid heroku command – Atif May 25 '16 at 11:51
  • Try `heroku apps:info` it should display web url for your app..that web url you need to use in browser.... – Dhananjaya Kuppu May 25 '16 at 12:04
  • @DhananjayaKuppu same result. require('./routes/main.routes.js'); this file does not execute. everything else is OK. And the open app link opens the same URL. – Atif May 25 '16 at 12:40
  • the server.js will execute only once, so your routes.js will also run only once.after that it listen on the specified port and only the callback function will get executed for every new request. but if there is any code that you want to execute for every request you shall be putting in the callback function.. – Dhananjaya Kuppu May 25 '16 at 12:49
  • where can I find call back function? – Atif May 25 '16 at 13:00
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/112919/discussion-between-atif-and-dhananjaya-kuppu). – Atif May 25 '16 at 13:00

1 Answers1

1

You can try this replace the comment (//write the code here) and try requesting the url:

var http = require('http'); 
 http.createServer(function (req, res) {
   // write the code here if it needs to execute every time
   res.writeHead(200, {'Content-Type': 'text/plain'});
   res.end("this is a test page");
 }).listen(process.env.PORT);
Dhananjaya Kuppu
  • 1,322
  • 9
  • 10
  • You are right man. Now it is working, But there is one problem. The code I placed there is inserting two records in database. when I run it in Firefox, it inserts exact two records, but when I run that app in chrome it inserts four records. Can you help with that? – Atif May 25 '16 at 13:59
  • could you please place the code that you have replaced with the comment, it would help me.... – Dhananjaya Kuppu May 25 '16 at 14:04