heroku server and firebase
<p>I am trying to make a server that listens to changes made in the firebase database and then sends an email to some email address. I manage to implement firebase and sendgrid, I already tested by adding some entry to the database and also sending a test mail. So sendgrid and firebase are working. The problem here relies on how I manage to send the emails, every time I open the app it sends the email.</p>

<p>So here comes the issue, If every time the apps opens it executes the code in the js file. When I add the code to listen to a child added event in the database it will get called every time I open the app, so multiple listener will be active and I assume multiple emails will be send regarding one single event. </p>

<p>So I'm clueless , is there anyway that this is only called once? or I should not open the "app" more than one time? or am I not deploying the server the right way?</p>

<p>My objective is that once I deploy the server it will automatically do whats its written, without the need to actually open the "app" in heroku, or at least that when the app is open the code do not get called again, just once when deployed.</p>

<p>Here is the code for the server.js</p>

<pre><code>var firebase = require('firebase');
var helper = require('sendgrid').mail;

firebase.initializeApp({
  serviceAccount: "./DB-A2312SDA.json",
  databaseURL: "https://DATABASENAME.firebaseio.com/"
});

// Email

var from_email = new helper.Email('example@example.com');
var to_email = new helper.Email('example@gmail.com');
var subject = 'Hello World from the SendGrid Node.js Library!';
var content = new helper.Content('text/plain', 'Hello, Email!');
var mail = new helper.Mail(from_email, subject, to_email, content);

var sg = require('sendgrid')(process.env.SG_API_KEY);
var request = sg.emptyRequest({
  method: 'POST',
  path: '/v3/mail/send',
  body: mail.toJSON(),
});

sg.API(request, function(error, response) {
  console.log(response.statusCode);
  console.log(response.body);
  console.log(response.headers);
});
</code></pre>

<p>This is the package.json</p>

<pre><code>{
  "name": "server_test",
  "version": "1.0.0",
  "description": "",
  "main": "server.js",
  "scripts": {
    "start": "node server.js"
  },
  "engines": {
    "node": "6.9.0"
  },
  "author": "",
  "license": "ISC",
  "dependencies": {
    "firebase": "^3.5.1",
    "sendgrid": "^4.7.0"
  }
}
</code></pre>

<p>and the procfile</p>

<pre><code>web: node server.js
</code></pre>