title: Correct method to send simple data using Node.js
body: <p>I have setup a basic Node.js server using express (WebStorm default), and have attempted to make it upon request (from a pebble watch) to run a python script, and send the returned json in the form: </p>
  
  <pre><code>{"willCollide": 1, "time": 6000, "strength": "NA"}
  </code></pre>
  
  <p>back to the watch. I have just started looking into JavaScript so have very little experience, and would expect I'm doing most of this incorrectly.
  Currently I experience an "Error: can't set headers after they are sent" and was wondering what is the correct method to send a json to a user upon a request?
  I am also wondering whether this is indeed the best method to go about sending the data from the python script to a Pebble watch.
  Below is the code in the JavaScript file being called on the request:</p>
  
  <pre><code>var express = require('express');
  var router = express.Router();
  var PythonShell = require('python-shell');
  
  var options = {
      mode: 'json'
  };
  
  var rain_data;
  
  function run_py_script(data){
  
      var pyshell = new PythonShell('dummy.py', options);
      var ret_val;
      /* Dummy data doesnt matter atm */
      pyshell.send("dummy data"); // change to data
  
      pyshell.on('message', function(message){
          console.log(message);
          ret_val = message;
          console.log(message["willCollide"]); // debug check
      });
  
      pyshell.end(function(err){
          if (err) {
              console.log('error received from python script');
          }
          console.log('finished script');
      });
  
      return ret_val;
  }
  
  /* GET rain_track data. */
  
  router.get('/', function(req, res, next) {
  
      rain_data = run_py_script(null);
  
      res.write(rain_data);
      res.end();
  });
  
  module.exports = router;
  </code></pre>