【问题标题】:File path in MeteorMeteor 中的文件路径
【发布时间】:2016-03-11 00:15:45
【问题描述】:

我无法识别公共目录 c:\TEMP\todos\.meteor\local\build\programs\server\public\main.py 中文件的路径。 Meteor 抱怨文件或目录不存在。已经搜索过有关类似问题的其他帖子(例如,Reading files from a directory inside a meteor app)但没有帮助。

这是错误信息。

=> 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 应该在 Meteor 应用程序的根目录下,而不是在 server 下。从客户端public 目录将解析为/,而不是/public

标签: javascript node.js meteor


【解决方案1】:

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

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

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

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

【讨论】:

  • 虽然您的回答没有解决我的问题,但它仍然是非常有用的信息。非常感谢您的帮助。
【解决方案2】:

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

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

【讨论】:

  • 如果您将代码部署到其他地方,那将无法正常工作,是吗?
  • 我猜这取决于它的部署位置。
【解决方案3】:

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

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-09-29
    • 2020-02-20
    • 2016-07-07
    • 1970-01-01
    相关资源
    最近更新 更多