2

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.com/2eqdp2o.jpg

Kil'jaeden
  • 939
  • 2
  • 11
  • 19

2 Answers2

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.org/api/path.html

Adam
  • 1,059
  • 9
  • 15
  • Removing the / bring me to C:\\Users\\deno_000\\form.html – Kil'jaeden Feb 26 '14 at 22:55
  • Yea as the last edit has mentioned your pathing is probably in the wrong format, expecting *nix paths but windows style paths are given. – Mouseroot Feb 26 '14 at 22:57
  • Where is the form.html on the local file system? Is C:\\Users\\deno_000\\ where the .js node file is? – Adam Feb 26 '14 at 22:59
  • make sure you start node from cmd.exe by changing to the project directory and doing node app.js – Adam Feb 26 '14 at 23:05
  • So, I should ransform the path to Unix-style and change the project directory? Is node.js really this Windows unfriendly.... – Kil'jaeden Feb 26 '14 at 23:11
  • node.exe and most unix applications will look for files in the current directory if they do not have a full path. The current directory will be the directory that your in when you start node. It comes down to how your starting node.js as to what your current directory is. Can you tell us how your starting node.js? – Adam Feb 26 '14 at 23:13
  • I have to manually change the directory to the correct place everytime I have to start a fresh new CMD. – Kil'jaeden Feb 26 '14 at 23:18
  • You can make a batch file(.bat) in the `app.js` directory with `node app.js` in it and when you double click that file in windows explorer it should use the .bat file directory as the current directory and start node in the app.js directory. I take it that it works if in the correct directory? – Adam Feb 26 '14 at 23:28
  • Yes, that's possible but that batch file does not recognize node commands. I think I just have to get Linux machine... – Kil'jaeden Feb 26 '14 at 23:34
  • http://i60.tinypic.com/23uzono.png shows what I mean. Just make sure the .bat file is in with the app.js file. – Adam Feb 26 '14 at 23:35
  • I think I gonna ask a more specific question about Windows directory and node.js tomorrow... because the question title might be too unrelevant to my real issue... – Kil'jaeden Feb 26 '14 at 23:52
  • Just a throw in for having `node.exe` always available, add the path here http://www.computerhope.com/issues/ch000549.htm. Then you only have to type `node app.js` *in* the folder where `app.js` is. ;) (Just chilling on the bed with my notebook, but I think Windows is hard to come by, watch: [Introduction to Node.js with Ryan Dahl](http://www.youtube.com/watch?v=jo_B4LTHi3I)) – loveNoHate Feb 26 '14 at 23:58
  • or you can just edit the batch file to say `C:\[location of node exe]\node.exe app.js` but path would be better if doing a lot of node development. – Adam Feb 27 '14 at 00:36
0

Had the same problem. Using __dirname fixed it.

Ashik
  • 1,204
  • 12
  • 22