Create a local Web service with node
1, create local file Server.js
varHTTP = require (' http ');varUrl=require (' url ');varFs=require (' FS ');varMine=require ('./mime ')). Types;varPath=require (' Path ');//Creating a serverHttp.createserver (function(Request, response) {varpathname =Url.parse (request.url). Pathname; varRealpath = Path.join ("Assets", pathname); varext =Path.extname (Realpath); if(ext = = "") {ext= ". html"; Realpath+ = "Index.html"; } ext= ext? Ext.slice (1): ' Unknown '; Fs.exists (Realpath,function(exists) {if(!exists) {Response.writehead (404, { ' Content-type ': ' Text/plain ' }); Response.Write ("This request URL" + pathname + "is not found on this server."); Response.End (); } Else{fs.readfile (Realpath,"Binary",function(err, file) {if(Err) {Response.writehead (500, { ' Content-type ': ' Text/plain ' }); Response.End (ERR); } Else { varContentType = Mine[ext] | | "Text/plain"; Response.writehead (200, { ' Content-type ': ContentType}); Response.Write (File,"Binary"); Response.End (); } }); } });}). Listen (8080);
2, create the load type Mime.js
Exports.types = { "CSS": "Text/css", "GIF": "Image/gif", "HTML": "Text/html", "ico": "Image/x-icon", "JPEG": "Image/jpeg", "JPG": "Image/jpeg", "JS": "Text/javascript", "JSON": "Application/json", "PDF": "Application/pdf", "PNG": "Image/png", "SVG": "Image/svg+xml", "SWF": "Application/x-shockwave-flash", "TIFF": "Image/tiff", "TXT": "Text/plain", "WAV": "Audio/x-wav", "WMA": "AUDIO/X-MS-WMA", "WMV": "Video/x-ms-wmv", "xml": "Text/xml", "MP3": "Audio/mpeg", "Ogg": "Audio/ogg", "Zip": "Application/zip"};
3. Create a Folder assets resources
4 node Server.js run
Create a Web server