只有在用户选择html中的文件后,才能在node.js中运行脚本
Run the script in node.js only after user select file in html
我正在使用node.js来启动一个服务,以便我的html可以与R代码通信。但我在node.js上遇到了一个问题。在我的html页面中,我有一个浏览选择按钮,用户可以使用它来选择他们想要读取到html中的数据文件,node.js会将文件名传递给R,因此R代码会从所选数据文件中读取数据,然后运行分析模型。但由于我对Node.js只有非常基本的了解,所以目前,r代码只有在打开以下链接"localhost:3000/vis/rio"时才会运行。所以我的问题是,当数据文件被选中时,如何让node.js在后台自动运行R代码。非常感谢您提前提供的帮助。
这是我的代码:
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脚本: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//这是获取文件名的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//这是用于连接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);
};
当您对服务器进行调用时,看起来您在任何时候都没有调用/vis/rio。
您需要在客户端对/vis/rio进行第二次调用,或者如果您想使用该部分,您可以在index.js中导入/要求该模块,并将其包含在getFileName中,然后在返回文件之前调用那里的函数。我对rio不是很熟悉,但我在您的代码中看不到该函数的任何访问点。
编辑:如果我理解你试图正确地做什么,你总是可以在第一次ajax调用的成功回调中发出第二个请求(to/vis/rio)。
相关文章:
- Node.js v6.2.0类扩展不是函数错误
- 如何使用 node.js 比较两个 json 数组
- Node.js's Buffer.writeFloatBE in Javascript
- node.js:setInterval()正在跳过调用
- 如何处理node.js节点mongodb中的连接和查询队列
- Node.js正在更改应用程序以使用集群模块
- 如何使用Node.js最有效地解析网页
- 在openshift node js应用程序中获取请求
- 正在删除node.js中已验证的网站
- 与运行长作业(javascript,node.js)的第三方API同步的最佳实践
- 在索引.html和应用.js [node.js] 之间共享变量
- Node.js中的JavaScript原型对象效率
- node.js请求数据事件未在CORS ajax调用中触发
- 使用node.js制作网站
- 使用node.js服务器中的evernote api获取note内容
- 有没有任何方法可以使用node-js从不同的机器打开浏览器
- 使用CI和CodeDeploy对node.js应用程序进行连续部署
- 在Node.js中上传和检索图像
- 什么's本地node.js服务器和python简单http服务器之间的区别
- 如何使用 Node JS 获取 JSON 嵌套数组值