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
/
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 ...
}