简体   繁体   English

仅当用户在html中选择文件后,才在node.js中运行脚本

[英]Run the script in node.js only after user select file in html

I am using node.js to launch a serve so that my html can communicate with R code. 我正在使用node.js启动服务,以便我的html可以与R代码进行通信。 But I am facing a problem on node.js. 但是我在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. 在我的html页面中,我有一个浏览选择按钮,用户可以使用它来选择要读取的数据文件,node.js会将文件名传递给R,因此R代码将从所选数据文件中读取数据然后运行分析模型。 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". 但是由于我只具有Node.js的非常基础的知识,所以目前,仅当我打开以下链接“ localhost:3000 / vis / rio”时,r代码才会运行。 So my question is how to make node.js run the R code in background automatically when the data file has been selected. 所以我的问题是,当选择了数据文件后,如何使node.js在后台自动运行R代码。 Thank you very much for your help in advance. 非常感谢您的提前帮助。

Here are my codes: 这是我的代码:

Javascript-(sending the file name to node.js): Javascript-(将文件名发送到node.js):

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);
    });

Node.js script: serve.js: Node.js脚本:serve.js:

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");
});

index.js // this is the js file for getting file name index.js //这是用于获取文件名的js文件

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};

vis.js // this is the file used to connect with Rserve and pass the name to R code vis.js //这是用于与Rserve连接并将名称传递给R代码的文件

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);
};

It looks like you aren't calling out to /vis/rio at any point when you make the call out to your server. 好像在任何时候都未调出/ vis / rio到服务器上。

You'll either need to make a second call on the client side to /vis/rio or if you want to use that section, you can import/require the module in index.js and include it in getFileName and just call out to the function there before you return the file. 您可能需要在客户端再次调用/ vis / rio,或者如果您想使用该部分,则可以在index.js中导入/获取该模块并将其包含在getFileName中,然后仅调用在返回文件之前,请先在此处执行功能。 I'm not super familiar with rio, but I don't see any access point in your code to that function. 我对rio不太熟悉,但是在您的代码中看不到该功能的任何访问点。

Edit: If I understand what you're trying to do correctly, you can always make a second request (to /vis/rio) in the success callback of your first ajax call. 编辑:如果我了解您要正确执行的操作,则始终可以在您的第一个Ajax调用的成功回调中发出第二个请求(至/ vis / rio)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM