Meteor中的文件路径

File path in Meteor

本文关键字:路径 文件 Meteor      更新时间:2023-09-26

我在识别公用目录c:''TEMP''todos''.metreor''local''build''programs''server''public''main.py中文件的路径时遇到问题。meteor抱怨该文件或目录不存在。已经搜索了关于类似问题的其他帖子(例如,从流星应用程序内的目录中读取文件),但没有帮助。

这是错误消息。

=> Your application has errors. Waiting for file change.
=> Modified -- restarting.
=> Meteor server restarted
W20151206-04:05:57.893(-5)? (STDERR) Error inside the Async.runSync: ENOENT, no such file or directory 'c:'TEMP'todos'.meteor'local'build'programs'server'public'

客户端代码

Meteor.call('runPython', function(err, response) {
    if(err){
    } else {
        console.log(response);
    }
})

服务器代码

Meteor.startup( function (){
    Meteor.methods({
        runPython: function (){
            var PythonShell = Meteor.npmRequire('python-shell');
            var fs = Meteor.npmRequire('fs');
            var runPython = Async.runSync(function (done){
                var files = fs.readdirSync('./public/');
//   PythonShell.run('main.py', function ... was tried first but Meteor complained that "main.py doesn't exist". So below is a different attempt.
                var py = _(files).reject(function(fileName){
                    return fileName.indexOf('.py') <0;
                })
            PythonShell.run(py, function (err) {
// PythonShell.run(path.join(py,"main.py") ... was also tried but resulted in the same error
                if (err) throw err;
                    console.log('script running failed');
                });
        })
        return "Success";
    }
})
})

应使用'/':读取public文件夹中的所有文件

var files = fs.readdirSync('/');

更多信息:http://docs.meteor.com/#/full/structuringyourapp

仅针对服务器端(可能是您的情况,也可能是更好的解决方案),您可以将所有内容放在private/文件夹下,并使用Assets API访问它们:http://docs.meteor.com/#/full/assets_getText

显然我想得太多了。我只需要指定文件的完整路径。

PythonShell.run('c:''project''public''main.py', function ...

如果您的应用程序允许将Python脚本移动到/private而不是/public,则可以利用Meteor的Assets:

Assets允许Meteor应用程序中的服务器代码访问静态服务器资产,这些资产位于应用程序树的私有子目录中。资产不会作为源文件进行处理,而是直接复制到应用程序的捆绑包中。

例如,如果您将脚本移动到/private/scripts/script.py,则可以通过执行Assets.absoluteFilePath('scripts/script.py')生成Meteor方式的绝对路径。