Contact Nodejs not deep, see the page every link to write a handler, like in the page display picture, or call external CSS, JS file, each link to write a handler, feel too troublesome, whether can write a program out, can automatically recognize pictures, CSS, JS file link, in the future to call the picture, external CSS, JS only need to care about how to write the front end, instead of the back. Therefore, the following procedure is available.
Index.js
varHTTP = require ("http");//Get HTTP Objectvarurl = require ("url");//Get URL ObjectvarPath = require ("path");varImgext =NewArray (". png", ". jpg", ". jpeg", ". bmp", ". gif");varCssext =NewArray (". css");varJsext =NewArray (". js"); Array.prototype.contain=function(obj) { for(vari=0; i< This. length; i++){ if( This[i] = = =obj)return true; } return false;};//http, the URL is the system comes with the module, and the following requesthandlers is our hand-written module, corresponding to the current directory of the Requesthandlers.js filevarRequesthandlers = require ("./requesthandlers");//The onrequest function, which handles HTTP requests, and different URL requests are handled by different functionsfunctiononrequest (Request, response) {varpathname = Url.parse (request.url). Pathname;//gets the requested URL //The handle property of Requesthandlers is also an object that contains multiple sets of properties: Property values, property names corresponding to URIs, and property values corresponding to the handler functions, see Requesthandlers.js if(typeofRequesthandlers.handle[pathname] = = = "function"{Requesthandlers.handle[pathname] (request, response); } //working with picture links Else if(Typeconfirm (Imgext,request.url)) {varext =Path.extname (Path.basename (Request.url)); Console.log ("Ext:" +ext); Requesthandlers.getimage (Request, response, pathname); } //working with CSS links Else if(Typeconfirm (Cssext,request.url)) {REQUESTHANDLERS.GETCSS (Request, response, pathname); } //Handling JS Links Else if(Typeconfirm (Jsext,request.url)) {Requesthandlers.getjs (Request, response, pathname); } Else{Console.log ("No Request handler found for" +pathname); Response.writehead (404, {"Content-type": "Text/html;charset=utf-8"}); Response.Write ("The page you visited does not exist!" Visit <a href= '/' > Home </a> '); Response.End (); }}http.createserver (ONrequest). Listen (8888);functiontypeconfirm (type,url) {varext =path.extname (path.basename (URL)); if(Type.contain (EXT)) {return true; } return false;}
Requesthandlers.js
varHandle = {};varFormidable = require ("Formidable");varFS = require ("FS"); handle["/"] =start;handle["/start"] =start;//read the file and outputfunctionStart (Request, Response) {Fs.readfile ("Html/index.html",function(err, HTML) {if(ERR)Throwerr; Response.writehead ($, {"Content-type": "Text/html"}); Response.Write (HTML); Response.End (); });}functiongetImage (Request, response, pathname) {//Here we need to remove the pathname "/" Symbol, Fs.readfile () to recognize, so use pathname.substring (1) //or, in front of the pathname, add the table's current directory's "." Recommended to use the followingFs.readfile ("." + Pathname,function(err, result) {if(ERR)Throwerr; Response.writehead ($, {"Content-type": "Image/jpeg"}); Response.Write (Result,"Binary"); Response.End (); });}functiongetcss (Request, response, pathname) {//This must be removed pathname the front "/" Symbol, Fs.readfile () to identify, so use pathname.substring (1)Fs.readfile ("." + Pathname,function(err, result) {if(ERR)Throwerr; Response.writehead ($, {"Content-type": "Text/css"}); Response.Write (Result); Response.End (); });}functionGetjs (Request, response, pathname) {//This must be removed pathname the front "/" Symbol, Fs.readfile () to identify, so use pathname.substring (1)Fs.readfile ("." + Pathname,function(err, result) {if(ERR)Throwerr; Response.writehead ($, {"Content-type": "Text/javascript"}); Response.Write (Result); Response.End (); });} Exports.handle=Handle;exports.start=Start;exports.getimage=Getimage;exports.getcss=Getcss;exports.getjs= Getjs;
Attention:
In the case of a reference image, the path to the SRC attribute in img should be relative to the project root directory . For example, root is the root directory, the path of an HTML file is root/web/test.html, and the picture is in Root/web/imges/test.png, the path should be written when the picture file is called/web/images/ Test.png, rather than directly using relative path images/test.png.
Nodejs processing pictures, CSS, JS links