Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Is there a Node.js function to read the contents of a file on a server?

My initial question got marked as a duplicate for a completely unrelated question that has absolutely nothing in common with my question, so I’m reposting this with some clarifications that should hopefully make it clearer what I want to do.


Suppose I have a file on a server that is NOT my server:

https://example.com/names.txt

Is there a function such that I can read this file as a String or as a Buffer?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

The obvious solution would be to download the file into a temporary directory, read its contents, then delete it, but this is very slow and takes many lines of code. Is there a cleaner way to do this?

>Solution :

You’d have to make a request to that server for that file. (e.g. a GET request) using the module of your choice.

Making a request with the HTTP module

Note: in the example, d in the on('data', ...) callback will be what you want.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading