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