nodejs的安装和使用
windows环境下安装使用nodejs
操作方法
- 01
下载nodejs的安装包 node-v0.10.29-x86.msi
- 02
双击并进行安装,路径可任意,任何目录都可以
- 03
安装好后,测试查看nodejs的版本
- 04
进入到目标工程下,并进行测试
- 05
server配置文件如下: console.time('[WebSvr][Start]'); var libHttp = require('http'); var libUrl=require('url'); var libFs = require("fs"); var libPath = require("path"); var funGetContentType=function(filePath){ var contentType=""; var ext=libPath.extname(filePath); switch(ext){ case ".html": contentType= "text/html"; break; case ".js": contentType="text/javascript"; break; case ".css": contentType="text/css"; break; case ".gif": contentType="image/gif"; break; case ".jpg": contentType="image/jpeg"; break; case ".png": contentType="image/png"; break; case ".ico": contentType="image/icon"; break; default: contentType="application/octet-stream"; } return contentType; } var funWebSvr = function (req, res){ var reqUrl=req.url; console.log(reqUrl); var pathName = libUrl.parse(reqUrl).pathname; if (libPath.extname(pathName)=="") { pathName+="/"; } if (pathName.charAt(pathName.length-1)=="/"){ pathName+="index.html"; } var filePath = libPath.join("./static",pathName); libPath.exists(filePath,function(exists){ if(exists){ res.writeHead(200, {"Content-Type": funGetContentType(filePath) }); var stream = libFs.createReadStream(filePath, {flags : "r", encoding : null}); stream.on("error", function() { res.writeHead(404); res.end("<h1>404 Read Error</h1>"); }); stream.pipe(res); } else { res.writeHead(404, {"Content-Type": "text/html"}); res.end("<h1>404 Not Found</h1>"); } }); } var webSvr=libHttp.createServer(funWebSvr); webSvr.on("error", function(error) { console.log(error); }); webSvr.listen(80,function(){ console.log('[WebSvr][Start] running at http://127.0.0.1:80/'); console.timeEnd('[WebSvr][Start]'); });
- 06
测试页面helloworld!