문제

I'm stumped.

So I'm attempting to read a file cross-domain through fs. lib. It's responding that it does not exist. I've attempted to open the file with my browser and it works just fine. Should I be using something different than fs?

I've tried adding a port http://www.mydomain.com:80/uploads/docs/doc.doc but that does not work either.

Running a vhost on plesk panel. (apache)

도움이 되었습니까?

해결책

Should I be using something different than fs?

Yes. The fs module is meant for interacting with the machine's local file systems, rather than handling network communication such as HTTP requests and responses.

You can use the http module, specifically http.get(), to request a file from an HTTP server:

http.get('http://www.mydomain.com/uploads/docs/doc.doc', function (res) {
    // process the response, `res`, as desired

    if (res.statusCode === 200) {
        // Example, piping the response to a file on disk:

        res.pipe(fs.createWriteStream(__dirname + '/doc.doc'));
    } else {
        console.error('The address is unavailable. (%d)', res.statusCode);
    }
});

res will be an http.IncomingMessage, which are stream.Readables.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top