This article shares the processing of js static resource file requests for your reference. The specific content is as follows
html file
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Document</title> <link rel="stylesheet" href="" type='text/css' charset='utf-8'> </head> <body> <div class='box' id='box'></div> <script charset='utf-8' src=''></script> </body> </html>
css file
*{ margin:0; padding:0; } html,body{ font-size:14px; color:#000; } .box{ margin:50px auto; width:300px; height:300px; background:#e3bd83; border:10px solid #e0f2be; }
JS Files
var box = ('box'); = function(){ = "red" }
Server file
var http = require('http'), fs = require('fs'), url = require('url'); //Create a servicevar server1 = (function(req,res){ //Resolve the directory name of the file in the client request address and the data content passed to the current server var urlObj = (,true), pathname = urlObj["pathname"], query = urlObj["query"]; //Abbreviation try catch exceptions to prevent the requested resource file from existing. We will terminate if we do not add try catch service. This is not good, so we add try catch to catch exception // Handle requests for static resource files (HTML/CSS/JS...) -> Front-end routing var reg = /\.(HTML|JS|CSS|JSON|TXT|ICO)/i; if((pathname)){ //Get the suffix name of the request file var suffix = (pathname)[1].toUpperCase(); //Get the MIME type of the current file based on the suffix name of the requested file var suffixMIME = "text/plain";//TXT text corresponding to switch(suffix){ case "HTML": suffixMIME = "text/html"; break; case "CSS": suffixMIME = "text/css"; break; case "JS": suffixMIME = "text/javascript"; break; case "JSON": suffixMIME = "application/json"; break; case "ICO": suffixMIME = "application/octet-stream"; break; } try{ //Read the content or code in the file according to the specified directory (the read content is in string format) var conFile = ("."+pathname,"utf-8"); //Rewrite the response header information: Tell the client's browser what kind of MIME type the content I return is, specify that the format of the returned content is utf-8, avoiding garbled code (200,{'content-type':suffixMIME+';charset=utf-8'}) //The content returned by the server to the client is also a string (conFile) }catch(e){ (404,{'content-type':'text/plain;charset=utf-8'}); ("request file is not found") } /* MIME type: Each resource file has its own identity type, for example: MIME of HTML file The MIME type of the type is "text/html". The css file is "text/css" The browser will render according to the MIME type of the code */ } // try{ // var con = ("."+pathname,"utf-8"); // (con); // }catch(e){ // ("request file is not find") // } // if(pathname==="/"){ // var con = ("./","utf-8"); // (con); // return; // } // if(pathname==="/"){ // con = ("./","utf-8"); // (con); // return; // } // if(pathname==="/"){ // con = ("./","utf-8"); // (con); // return; // } }) //Configure the port for the current service(80,function(){ ("server is success,listening on 80"); })
The above is all the content of this article. I hope it will be helpful to everyone's study and I hope everyone will support me more.