Tol*_*lio 2 node.js express firebase
我有一个在 Firebase 上运行的expressjs 应用程序,firebase-express/functions/index.js我正在尝试读取位于 的文件functions/api/swagger/swagger.yaml。
const swaggerDocument = YAML.load('./api/swagger/swagger.yaml');
Run Code Online (Sandbox Code Playgroud)
当我在本地运行时firebase serve,出现以下错误。
Error: ENOENT: no such file or directory, open 'C:\Users\Tulio\Desktop\firebase-express\api\swagger\swagger.yaml'
at Error (native)
at Object.fs.openSync (fs.js:642:18)
at Object.fs.readFileSync (fs.js:510:33)
at Function.Utils.getStringFromFile (C:\Users\Tulio\Desktop\firebase-express\functions\node_modules\yamljs\lib\Utils.js:284:19)
at Function.Yaml.parseFile (C:\Users\Tulio\Desktop\firebase-express\functions\node_modules\yamljs\lib\Yaml.js:46:21)
at Function.Yaml.load (C:\Users\Tulio\Desktop\firebase-express\functions\node_modules\yamljs\lib\Yaml.js:78:17)
at Object.<anonymous> (C:\Users\Tulio\Desktop\firebase-express\functions\index.js:11:30)
at Module._compile (module.js:577:32)
at Object.Module._extensions..js (module.js:586:10)
at Module.load (module.js:494:32)
Run Code Online (Sandbox Code Playgroud)
它正在寻找位于 的文件'C:\Users\Tulio\Desktop\firebase-express\api\swagger\swagger.yaml',因此解决方案必须是更改我的参数以包含文件夹“functions”,对吧?
const swaggerDocument = YAML.load('./functions/api/swagger/swagger.yaml');
Run Code Online (Sandbox Code Playgroud)
现在我收到另一个错误,它将路径显示为:
no such file or directory, open 'C:\Users\Tulio\Desktop\firebase-express\functions\functions\api\swagger\swagger.yaml'
Run Code Online (Sandbox Code Playgroud)
注意到“函数\函数”了吗?由于某种原因,当我将“functions/”添加到路径时,它会附加另一个“functions/”,使路径无效。
如何获取文件functions夹内文件的正确路径?
使用路径库
var path = require('path');
var swagger_path = path.resolve(__dirname,'./api/swagger/swagger.yaml');
console.log(swagger_path);
Run Code Online (Sandbox Code Playgroud)
这将在任何环境中获取正确的路径。
| 归档时间: |
|
| 查看次数: |
2143 次 |
| 最近记录: |