最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Node.js fs.ReadFile always returns error - Stack Overflow

programmeradmin0浏览0评论

I want Node.js to read form.html when the domain name is localhost:3000/form, but for some reason, it always gives me an error 500 page.

The content parameter in the callback function of fs.readFile gets undefined, even though the path of the file is correct.

app.get('/form', function(req, res){
    fs.readFile('/form.html', function(error, content){
        if(error){
            // This get's always executed... I don't know why.
            // content = undefined.
            res.writeHead(500);
            res.end();
        }
        else{
            res.writeHead(200, { 'content-type' : 'text/html' });
            processFile(content);
            res.end(content, 'utf-8');
        }
    });
});

added error message:

{ [Error: ENOENT, open 'C:\form.html'] errno: 34, code: 'ENOENT', path: 'C:\form.html' }

Do I have to specify the full path to the file...?

After I removed the / I get this path:

C:\Users\deno_000\form.html

My files are all in the same directory, and on the left side of my editor you can see it:

.jpg

I want Node.js to read form.html when the domain name is localhost:3000/form, but for some reason, it always gives me an error 500 page.

The content parameter in the callback function of fs.readFile gets undefined, even though the path of the file is correct.

app.get('/form', function(req, res){
    fs.readFile('/form.html', function(error, content){
        if(error){
            // This get's always executed... I don't know why.
            // content = undefined.
            res.writeHead(500);
            res.end();
        }
        else{
            res.writeHead(200, { 'content-type' : 'text/html' });
            processFile(content);
            res.end(content, 'utf-8');
        }
    });
});

added error message:

{ [Error: ENOENT, open 'C:\form.html'] errno: 34, code: 'ENOENT', path: 'C:\form.html' }

Do I have to specify the full path to the file...?

After I removed the / I get this path:

C:\Users\deno_000\form.html

My files are all in the same directory, and on the left side of my editor you can see it:

http://i59.tinypic./2eqdp2o.jpg

Share Improve this question edited Feb 26, 2014 at 23:08 Kil'jaeden asked Feb 26, 2014 at 22:39 Kil'jaedenKil'jaeden 9692 gold badges11 silver badges21 bronze badges 7
  • 2 It would help if you log the error and post here the error message. – heyheyjp Commented Feb 26, 2014 at 22:42
  • also - just a guess but maybe it's './form.html' unless that's in the root – bryanmac Commented Feb 26, 2014 at 22:46
  • Oke I added the error message, looks like I have to specify the full path? – Kil'jaeden Commented Feb 26, 2014 at 22:49
  • not the full path just remove the /, node thinks you are saying the file is on the root of your fs, removing the / will tell node to use the relative path. – Mouseroot Commented Feb 26, 2014 at 22:50
  • It is still not the right path... it bring me to: C:\\Users\\deno_000\\form.html – Kil'jaeden Commented Feb 26, 2014 at 22:52
 |  Show 2 more ments

3 Answers 3

Reset to default 2

/ in most file systems = root directory.

Either remove the / or add a dot infront like form.html or ./form.html.

. is the current directory

.. is the parent directory

./form.html = [current directory]/form.html]

The error is similar to file not found.

The html file would need to be in the same folder as the .js node file for this to work. If you have it in another path, use that path. \

Note you can also use:

Path#

Stability: 3 - Stable This module contains utilities for handling and transforming file paths. Almost all these methods perform only string transformations. The file system is not consulted to check whether paths are valid.

Use require('path') to use this module.

http://nodejs/api/path.html

Had the same problem. Using __dirname fixed it.

const filePath = path.join(__dirname, 'form.html'); // Absolute path to form.html
   
fs.readFile(filePath, function(error, content) {
    // rest of your code ...

}
发布评论

评论列表(0)

  1. 暂无评论