Node.js - a resource interpreted as Script, but ported with the MIME type text / plain

First: I do not use Express.

With this in mind, when I load the index.html file, it recursively readFile every attached file, such as my CSS and JS pages. But it always returns this error in my inspector (Chrome):

The resource is interpreted as Script, but passed with the MIME type text / plain

I have absolutely no idea why this is being done. Here is my code:

 var http = require('http'); var querystring = require('querystring'); var fs = require('fs'); var url = require('url'); 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 { var file_path = ""; // parses the url request for a file and pulls the pathname var url_request = url.parse(request.url).pathname; var tmp = url_request.lastIndexOf("."); var extension = url_request.substring((tmp + 1)); file_path = url_request.replace("/", ""); //load needed pages and static files fs.readFile(file_path, function (error, contents){ if(error){ console.log('DIE!'); console.log(error); response.writeHeader(500, {"Content-Type": "text/html"}); response.end("<h1>FS READ FILE ERROR: Internal Server Error!</h1>"); } else{ console.log('SUCCESS!'); // set content type if (extension === 'html') response.writeHeader(200, {"Content-Type": 'text/html'}); else if (extension === 'htm') response.writeHeader(200, {"Content-Type": 'text/html'}); else if (extension === 'css') response.writeHeader(200, {"Content-Type": 'text/css'}); else if (extension === 'js') response.writeHeader(200, {"Content-Type": 'text/javascript'}); else if (extension === 'png') response.writeHeader(200, {"Content-Type": 'image/png'}); else if (extension === 'jpg') response.writeHeader(200, {"Content-Type": 'image/jpg'}); else if (extension === 'jpeg') response.writeHeader(200, {"Content-Type": 'image/jpeg'}); else { console.log("NO CORRECT EXTENSION")}; console.log(extension); response.end(contents); } response.end(); }); response.end(); } } exports.route = route; 

How can i fix this? This stopped my project.

Note: this is a progression of the earlier issue that I talked about here: Node.js wrong path issues

+6
source share
2 answers

Try commenting out the last answer .end. Do not think that you need it.

  response.end(); }); // response.end(); } exports.route = route; 

I suspect that response.end is called just before the one in the fs.readfile callback manages to execute, perhaps it closes the headers on the server without specifying the correct header β€œContent-Type”: 'text / javascript' chance get sent.

I tried it according to your code, worked for me ..

+8
source

If nodejs works for me and uses express, then the code below

  if (extension === 'html') res.set("Content-Type": 'text/html'); else if (extension === 'htm') res.set("Content-Type": 'text/html'); else if (extension === 'css') res.set("Content-Type": 'text/css'); else if (extension === 'js') res.set("Content-Type": 'text/javascript'); else if (extension === 'png') res.set("Content-Type": 'image/png'); else if (extension === 'jpg') res.set("Content-Type": 'image/jpg'); else if (extension === 'jpeg') res.set("Content-Type": 'image/jpeg'); 

The above code works from me.

0
source

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


All Articles