我正在编写一个流星包“myPackage”,它需要使用 Npm FileSystem 和 Pah 模块将文件写入磁盘。
该文件最终应位于 example-app/packages/myPackage/auto_ generated/myFile.js 中,其中 example-app 项目
已添加 myPackage。
fs = Npm.require( 'fs' ) ;
path = Npm.require( 'path' ) ;
Meteor.methods( {
autoGenerate : function( script ) {
var myPath = '/Users/martinfox/tmp/auto-generated' ;
var filePath = path.join(myPath, 'myFile.js' ) ;
console.log( filePath ) ; // shows /Uses/martinfox/tmp/auto-generated/myFile.js
var buffer = new Buffer( script ) ;
fs.writeFileSync( filePath, buffer ) ;
},
} );
当我运行上面的代码(仅服务器端)时,我得到
Exception while invoking method 'autoGenerate' Error: ENOENT,
no such file or directory '/Uses/martinfox/tmp/auto-generated/myFile.js'
注意 /Uses/martinfox/tmp/auto-generate 文件夹确实存在
- 任何想法出了什么问题吗?
- 是否可以获得meteor项目目录的绝对路径?
要获取项目的路径,您可以执行以下操作:
来自存储在应用程序根目录中的 main.js
var fs = Npm.require('fs');
__ROOT_APP_PATH__ = fs.realpathSync('.');
console.log(__ROOT_APP_PATH__);
您还可以检查您的文件夹是否存在:
if (!fs.existsSync(myPath)) {
throw new Error(myPath + " does not exists");
}
希望它能帮助你
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)