Reputation: 180
I have created a server on node and I can render a HTML page if that page is inside the working folder.
However , when I change the path to a different folder , I can't render the page and I understand this has something to do with the path I am assigning.
This is the path to the main/working folder :
function OnRequest(request , response){
fs.readFile('./index.html' , null , function(error , data){
if (error){
response.writeHead(404);
console.log("Ups, something is wrong :/");
} else {
response.writeHead(200)
response.write(data)
response.end();
}
})
};
And this is the path to the folder outside the main folder :
function OnRequest(request , response){
fs.readFile('c:\Users\Simon\Desktop\Projectos\7 - Site da Pintura\Galeria.html' , null , function(error , data){
if (error){
response.writeHead(404);
console.log("Ups, something is wrong :/");
} else {
response.writeHead(200)
response.write(data)
response.end();
}
})
};
This is the error I got :
[Error: ENOENT: no such file or directory, open 'C:\Users\Simon\Desktop\Projectos\8 - Criar um Servidor\UsersSimonDesktopProjectos - Site da PinturaGaleria.html'] {
errno: -4058,
code: 'ENOENT',
syscall: 'open',
path: 'C:\\Users\\Simon\\Desktop\\Projectos\\8 - Criar um Servidor\\UsersSimonDesktopProjectos\x07 - Site da PinturaGaleria.html'
}
This makes me think that I need to change the path inside that object because obviously it is wrong. But how can I do that?
Upvotes: 0
Views: 4890
Reputation: 3710
Your problem is with relative or absolute paths, it's with string escaping backslashes (\
). In other words, you cannot simply use backlashes in string, as they are meant for escape characters like \'
(an quote without closing your string encapsulation), or \n
(new line).
To use a backslash in a string, you either need to write \\
:
const path = `C:\\Users\\Simon\\Desktop\\Projectos\\7 - Site da Pintura\\Galeria.html`
Or use String.raw to prevent your backslashes escaping:
const path = String.raw`C:\Users\Simon\Desktop\Projectos\7 - Site da Pintura\Galeria.html`
In either case, both paths should work fine as long as the file exists and within your read permissions.
Upvotes: 2
Reputation: 180
It seems I have found a solution. :
I assume one of the problems was that - even though I was giving the full path to the file I wanted to open in the readfFile() method - that path was being added to an already existent path in some object , resulting in this :
path: 'C:\\Users\\Simon\\Desktop\\Projectos\\8 - Criar um Servidor\\UsersSimonDesktopProjectos\x07 - Site da PinturaGaleria.html'
}
By using the method path.resolve() the issue seems to be gone and I can now load the HTML file outside from my home folder without even having to write the full path :
path.resolve('../7 - Site da Pintura/Galeria.html')
And it worked :
function OnRequest(request , response){
fs.readFile(path.resolve('../7 - Site da Pintura/Galeria.html'), null , function(error , data){
if (error){
response.writeHead(404);
console.log(error);
} else {
response.writeHead(200)
response.write(data)
response.end();
}
})
};
Upvotes: -1