1

Im using the following code to read simple file from my desktop

module.exports = function (app) {
    app.get('/aa', function(req, res) {

        fs = require('fs');
        fs.readFile('‪C:\\Users\\t56789\\Desktop\\myfile.txt', 'utf8', function (err,data) {
            if (err) {
                return console.log(err);
            }
            console.log(data);
            res.send(data);
        });

    });

when I hit in the browser /aa i got the following error

{ [Error: ENOENT, open 'C:\Users\t56789\WebstormProjects\Ro0.1\?C:\Users\t56789\Desktop\myfile.txt']
  errno: -4058,
  code: 'ENOENT',
  path: 'C:\Users\t56789\WebstormProjects\Ro0.1\?C:\Users\t56789\Desktop\myfile.txt' }

if I return instead res.send("test") i able to see in the browser test... any idea what I miss here ?

7
  • You're running a web server. The root of the web server is the directory it runs in. You can't directly read the C: drive. You can only read what's inside that root directory. Commented Jun 5, 2015 at 7:01
  • @JeremyThille-what does it mean running web server(express?) and if there is other way to do it? I fairly new to nodeJS... Commented Jun 5, 2015 at 7:06
  • Well NodeJS is a web server... just like Apache. If you run it from c:/myWebsite, Node will only be able to read what is inside c:/myWebsite. Only your website's files, not the whole disk. Unless I'm mistaken. Commented Jun 5, 2015 at 7:06
  • use path module to normalize path according Your OS requirements. and also read this article: shapeshed.com/writing-cross-platform-node Commented Jun 5, 2015 at 7:08
  • I think so. Cuz web server cannot deny You to get access to files above (it's files systems job) it's root, especially if You're using windows where Your user already is root (: Commented Jun 5, 2015 at 7:28

1 Answer 1

1

Because of the security reasons the web server is limited to it's current directory. So if you want to reach some file move it first to the web server's directory. Just to prove the concept you could try this code:

app.get('/aa', function (req, res) {
    var fs = require('fs');
    fs.readFile(__filename, 'utf8', function (err, data) {
        if (err) {
            return console.log(err);
        }
        console.log(data);
        res.send(data);
    });
});

It will display the content of the file where the code is.

Sign up to request clarification or add additional context in comments.

3 Comments

Thanks but where should I configure the __filename ?
__filename is a Node.js default variable that always shows the current file path. It is not you who do the configuration. You can read more about nodejs.org/api/fs.html or nodejs.org/api/path.html
It is not possible the file to be on the desktop you must manually copy/move the file in the web server directory.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.