title: Run the script in node.js only after user select file in html
body: <p>I am using node.js to launch a serve so that my html can communicate with R code. But I am facing a problem on node.js. In my html page, I have a browse selection button, user can use it to choose the data file they want to read into html and node.js will pass the file name to R, so R code will read data from the selected data file and then run the analytics model. But as i only have very basic knowledge of Node.js, so currently, r code would run only when I open the followling link "localhost:3000/vis/rio". So my question is how to make node.js run the R code in background automatically when the data file has been selected. Thank you very much for your help in advance. </p>
  
  <p>Here are my codes:</p>
  
  <p><strong>Javascript-(sending the file name to node.js):</strong></p>
  
  <pre><code>var dataset,availableTags;
      function handleFileSelect(evt) {
        var file = evt.target.files[0];
        $.ajax({            //getting the file name and update to node.js
           type:'post',  
           url:"/getFileName",  
           data:{filename:file.name}
        }); 
        Papa.parse(file, {     //papa is a library I used to read csv file
          header: true,
          dynamicTyping: true,
          complete: function(results) {
          dataset=results.data;
          }
        });
      }
  
      $(document).ready(function(){
        $("#csv-file").change(handleFileSelect);
      });
  </code></pre>
  
  <p><strong>Node.js script:
         serve.js:</strong> </p>
  
  <pre><code>var express=require('express');
  var path = require('path');
  var vis = require('./routes/vis');
  var index = require('./routes/index');
  var bodyParser=require('body-parser');
  var app=express();
  require('./routes/main')(app);
  app.get('/vis/rio',vis.rio);       **//this is a package used to get connection with Rserve**
  app.set('views',__dirname + '/views');
  app.set('view engine', 'ejs');
  app.engine('html', require('ejs').renderFile);
  app.use(express.static(path.join(__dirname, 'public')));
  app.use(bodyParser.urlencoded());              
  app.post("/getFileName",index.getFileName);    **//this is the script to get the file name, it is from index.js** 
  var server=app.listen(3000,function(){
  console.log("Express is running on port 3000");
  });
  </code></pre>
  
  <p><strong>index.js</strong> // this is the js file for getting file name</p>
  
  <pre><code>var express = require('express');
  var router = express.Router();
  
  /* GET home page. */
  router.get('/', function(req, res) {
    res.render('index', { title: 'Express' });
  });
  
  getFileName=function (req,res){
      global.filename=req.body.filename; **//this is the global variable which stores the file name**
      res.send('true');
  };
  
  module.exports = {router:router,getFileName:getFileName};
  </code></pre>
  
  <p><strong>vis.js</strong> // this is the file used to connect with Rserve and pass the name to R code</p>
  
  <pre><code>var rio = require("rio");
  var arg={};
  
  exports.rio = function(req, res){
      arg={names:[global.filename]};
      console.log(arg);
       options = {
          entryPoint:"nameoffile",
          data: arg,
          host : "127.0.0.1",
          port : 6311,
          callback: function (err, val) {
              if (!err) {
                   console.log("RETURN:"+val);
                   return res.send({'success':true,'res':val});
              } else {
                   console.log("ERROR:Rserve call failed")
                   return res.send({'success':false});
              }
          },
      }
      rio.enableDebug(true);
      rio.sourceAndEval(__dirname + "/Rcode/test.R",options);
  };
  </code></pre>