Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/474.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/36.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何使用node.js在服务器上打开文件_Javascript_Node.js_Amazon Ec2_Fs - Fatal编程技术网

Javascript 如何使用node.js在服务器上打开文件

Javascript 如何使用node.js在服务器上打开文件,javascript,node.js,amazon-ec2,fs,Javascript,Node.js,Amazon Ec2,Fs,我使用Node.js脚本读取并返回.txt文件中的随机行,使用以下函数: function pickMeal(){ var meals = fs.readFileSync('meals.txt','utf8'); var mealsArray = meals.toString().split('\n'); return mealsArray[Math.floor(Math.random()*mealsArray.length)]; } 在我的桌面上,它工作正常。当我将.js文件和.txt文件都

我使用Node.js脚本读取并返回.txt文件中的随机行,使用以下函数:

function pickMeal(){
var meals = fs.readFileSync('meals.txt','utf8');
var mealsArray = meals.toString().split('\n');
return mealsArray[Math.floor(Math.random()*mealsArray.length)];
}
在我的桌面上,它工作正常。当我将.js文件和.txt文件都上传到服务器上的文件夹中时,突然出现了一个
错误:enoint,没有这样的文件或目录“fines.txt”

我使用的是AmazonEC2服务器,.js脚本和.txt文件位于同一目录中。可能是.txt文件的权限有问题

我已经尝试将脚本定向到
~/folder/feeds.txt
甚至文件的整个http链接,但我得到了相同的结果


谢谢你的帮助

节点将尝试从系统根目录中读取文件。您必须给出完整的路径名

var path=require('path');
var txtpath=path.join(xtfile dirname,'folder_where_txt_file_'is,'txtfile.txt');
//现在您可以读取txt路径