varHTTP = require ("http"), URL= Require ("url"), Path= Require ("path"), FS= Require ("fs"); Http.createserver (function(req, res) {varpathname = __dirname +Url.parse (req.url). pathname; if(path.extname (pathname) = = "") {pathname+= "/"; } if(pathname.charat (pathname.length-1) = = "/") {pathname+ = "index.html"; } if(path.isabsolute (pathname)) {Switch(path.extname (pathname)) { case". html": Res.writehead (200, { "content-type": "text/html" }); break; case". js": Res.writehead (200, { "content-type": "text/javascript" }); break; case". css": Res.writehead (200, { "content-type": "text/css" }); break; case". gif": Res.writehead (200, { "content-type": "image/gif" }); break; case". jpg": Res.writehead (200, { "content-type": "image/jpeg" }); break; case". png": Res.writehead (200, { "content-type": "image/png" }); break; default: Res.writehead (200, { "content-type": "application/octet-stream" }); } fs.readfile (pathname,function(err, Data) {res.end (data); }); } Else{res.writehead (404, { "content-type": "text/html" }); Res.end ("); }}). Listen (8124, "127.0.0.1"); Console.log ("Server running at http://127.0.0.1:8124/");
Nodejs build a simple web server!!