简体   繁体   English

node.js如何从远程服务器读取流中的文件

[英]node.js how to read file in stream from remote server

In node.js I am trying to watch and read a text file on filechange.For this, I am using below code and it is working absolutely fine. 在node.js中,我试图在filechange上观察和读取文本文件,为此,我正在使用以下代码,并且运行良好。

var fs = require('fs'),bite_size = 256,readbytes = 0,file;
fs.open('testread.txt', 'r', function(err, fd) {
    file = fd; //readsome(); 

    var func = (function readsome() {
    var stats = fs.fstatSync(file); // yes sometimes async does not make sense!
    if(stats.size<readbytes+1) {        
        setTimeout(readsome, 1000);
    }
    else {
        fs.read(file, new Buffer(bite_size), 0, bite_size, readbytes, function (err, bytecount, buff) {
        console.log(buff.toString('utf-8', 0, bytecount));
        readbytes+=bytecount;
        process.nextTick(readsome);
        });
    }
    })();   
});

But this works only for local file and now I need to read text file from remote server ( may be linux or windows) in same way. 但这仅适用于本地文件,现在我需要以相同方式从远程服务器(可能是linux或Windows)读取文本文件。

Please help , how can I use remote server login information like below 请帮助,如何使用如下所示的远程服务器登录信息

IP address = "XXX.XXX.XX.XX";, 
username = username, 
password = password, 
filepath = "/root/testread.txt" or "D:\testread.txt" 

and read the data from text file present at filepath in remote server. 并从远程服务器文件路径中存在的文本文件中读取数据。

fs uses the local filesystem. fs使用本地文件系统。 To access remote files you need to use some network protocol (NTFS for Windows and ssh or ftp for linux for example) Those protocols expose files to remote access, you cannot access the filesystem of a remote host not using them 要访问远程文件,您需要使用某些网络协议(例如Windows的NTFS和linux的ssh或ftp的网络协议),这些协议将文件暴露给远程访问,您不能访问不使用它们的远程主机的文件系统

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM