Node.js the wrong way

When I do fs.readFile(file... , I get this error:

{[Error: ENOENT, open 'foundation / css / normalize.css']

errno: 34,

code: 'ENOENT',

path: 'foundation / css / normalize.css'}

Do I need to route my path in relation to the HTML file with which they are associated, or in relation to the location of the node.js files on which the server and router are running?

And no, I do not use express and “using express” is not an acceptable answer.


Edit

@BenjaminGruenbaum last comment. I'm not sure what you mean by my HTTP response, but my code logic is as follows: I load my index.html via a URI, and when it readFile , it recursively goes through each linked file. I have a script that breaks it in order to display the right Content-Type correctly. I will include it here because it is too long for a comment:

 function route(handle, pathname, response, request){ // console.log("About to route a request for " + pathname); if (typeof handle[pathname] === "function"){ handle[pathname](response, request); } else { // console.log('NON HTML'); // console.log(pathname); var file_path = ""; var mimes = { 'css': 'text/css', 'js': 'text/javascript', 'htm': 'text/html', 'html': 'text/html', 'png': 'image/png', 'jpg': 'image/jpg', 'jpeg': 'image/jpeg' }; // parses the url request for a file and pulls the pathname var url_request = url.parse(request.url).pathname; // console.log('URL REQUEST'); // console.log(url_request); // finds the placement of '.' to determine the extension var tmp = url_request.lastIndexOf("."); // determines the extension by uing .substring that takes everything after '.' var extension = url_request.substring((tmp + 1)); // console.log('EXTENSION'); // console.log(extension); //set path of static pages if (extension === 'css' || extension === 'js' || extension === 'htm' || extension === 'html' || extension === 'png' || extension === 'jpg' || extension === 'jpeg'){ // console.log('LOAD STATIC PAGE'); file_path = url_request.replace("/", ""); // console.log(file_path); // console.log(); } //load needed pages and static files fs.readFile(file_path, function (error, data){ if(error){ // console.log("AM I DEAD"); // console.log(error); // console.log(); response.writeHeader(500, {"Content-Type": "text/html"}); response.write("<h1>FS READ FILE ERROR: Internal Server Error!</h1>"); } else{ response.writeHeader(200, {"Content-Type": mimes[extension]}); console.log('SHOULD BE SUCCESS') console.log(mimes[extension]); // console.log(data); response.write(data); } response.end(); }); response.end(); } } exports.route = route; 

As you can tell how many places I have console.log() , I can track everything to the very end. Here fails:

 else{ response.writeHeader(200, {"Content-Type": mimes[extension]}); console.log('SHOULD BE SUCCESS') console.log(mimes[extension]); // console.log(data); response.write(data); } 

I apologize for the somewhat careless indentation.


Final editing:

A possible solution to this stupid situation can be found on the following question: Node.js is a resource interpreted as a script, but ported with the MIME type text / plain

Thanks a lot @BenjaminGruenbaum for getting up with Noob

+1
source share
1 answer

You will need to use the path relative to your NodeJS files (entry point more specifically), and not your client HTML code.

It looks like it works in bash scripts, in Python, and pretty much any other language using the standard API, which leads to reasonable calls to the native file system functions in the operating system.

In fact, the client-side HTML client has little in common with the fact that in Node, in Node it is indicated that it simply sends an HTTP message to the client. He does not know about the server’s internal file system at all.

(Also, I don't see something like express even help)

+2
source

Source: https://habr.com/ru/post/950863/


All Articles