node.js - 如何在无服务器中获取项目内部的文件路径
问题描述
我有一个节点 lambda 函数,可以读取其中的打包文件的内容。目录结构如下。
example1-folder // folder inside project
-> fns // in this folder all the functions/Lambda are written
-> index.html // the file which is trying to read the file
example2-folder
-> fns // in this folder all the functions/Lambda are written
-> index.html
data.txt // in the main project directory the file is present
然后在 example1-folder => index.js 文件中,我编写了以下代码来获取文件的路径。
let path = require("path");
exports.handler = (event, context, callback) => {
fs.readFile(__dirname +'/data.txt', function (err, data) {
if (err) throw err;
});
};
我不断收到以下错误:
[Error: ENOENT: no such file or directory, open '/var/task/data.txt']
请帮我解决问题。如果需要更多信息,请告诉我。
解决方案
推荐阅读
- laravel - 如何仅在 laravel 中获取当前用户数据
- node.js - Autodesk 查看 MongoDB 数据库中的保存标记
- javascript - 滚动自动javascript
- java - 如何在客户端 Java 应用程序中使用客户端证书?
- c++ - 如何在另一个 DLL 中将 SDL2 函数用于主可执行文件?
- angular - MyComponent 类型是 2 个模块声明的一部分:AppSharedModule 和 AppSharedModule
- javascript - 服务器上某些 Ajax 请求的错误 500
- go - 如何锁定功能
- unity3d - Unity3d回滚到以前的状态?
- ansible - Ansible:可自定义的模板文件名