使ç¨Nodejsæ建Webæå¡å¨æ¯å¦ä¹ Node.jsæ¯è¾å
¨é¢çå
¥é¨æç¨ï¼å 为è¦å®æä¸ä¸ªç®åçWebæå¡å¨ï¼ä½ éè¦å¦ä¹ Nodejsä¸å 个æ¯è¾éè¦ç模åï¼æ¯å¦ï¼httpå议模åãæ件系ç»ãurl解æ模åãè·¯å¾è§£æ模åã以å301éå®åé®é¢ï¼ä¸é¢æ们就ç®å讲ä¸ä¸å¦ä½æ¥æ建ä¸ä¸ªç®åçWebæå¡å¨ã
ä½ä¸ºä¸ä¸ªWebæå¡å¨åºå
·å¤ä»¥ä¸å 个åè½ï¼
1ãè½æ¾ç¤ºä»¥.html/.htmç»å°¾çWeb页é¢
2ãè½ç´æ¥æå¼ä»¥.js/.css/.json/.textç»å°¾çæ件å
容
3ãæ¾ç¤ºå¾çèµæº
4ãèªå¨ä¸è½½ä»¥.apk/.docx/.zipç»å°¾çæ件
5ãå½¢å¦
http://xxx.com/a/b/ , åæ¥æ¾bç®å½ä¸æ¯å¦æindex.html,å¦ææå°±æ¾ç¤ºï¼å¦æ没æå°±ååºè¯¥ç®å½ä¸çæææ件åæ件夹ï¼å¹¶å¯ä»¥è¿ä¸æ¥è®¿é®ã
6ãå½¢å¦
http://xxx.com/a/b, åä½301éå®åå°
http://xxx.com/a/b/ , è¿æ ·å¯ä»¥è§£å³å
é¨èµæºå¼ç¨éä½çé®é¢ã
å¼å
¥éè¦ç¨å°çå 个模åï¼
//httpå议模å
var http = require('http');
//url解æ模å
var url = require('url');
//æ件系ç»æ¨¡å
var fs = require("fs");
//è·¯å¾è§£æ模å
var path = require("path");
å建æå¡å¹¶å¨æå®ç端å£çå¬ï¼
//å建ä¸ä¸ªæå¡
var httpServer = http.createServer(this.processRequest.bind(this));
//å¨æå®ç端å£çå¬æå¡
httpServer.listen(port,function(){
console.log("[HttpServer][Start]","runing at http://"+ip+":"+port+"/");
console.timeEnd("[HttpServer][Start]");
});
å¨å建æå¡çæ¶åéè¦ä¼ éä¸ä¸ªå¿åå½æ°processRequest 对请æ±è¿è¡å¤çï¼processRequestæ¥æ¶2个åæ°ï¼åå«æ¯requeståresponse, request对象ä¸å
å«äºè¯·æ±çææå
容ï¼requestæ¯ç¨æ¥è®¾ç½®ååºå¤´ä»¥å对客æ·ç«¯ååºååºæä½ã
processRequest:function(request,response){
var hasExt = true;
var requestUrl = request.url;
var pathName = url.parse(requestUrl).pathname;
//对请æ±çè·¯å¾è¿è¡è§£ç ï¼é²æ¢ä¸æä¹±ç
pathName = decodeURI(pathName);
//å¦æè·¯å¾ä¸æ²¡ææ©å±å
if(path.extname(pathName) === ''){
//å¦æä¸æ¯ä»¥/ç»å°¾çï¼å /并ä½301éå®å
if (pathName.charAt(pathName.length-1) != "/"){
pathName += "/";
var redirect = "http://"+request.headers.host + pathName;
response.writeHead(301, {
location:redirect
});
response.end();
}
//æ·»å é»è®¤ç访é®é¡µé¢,ä½è¿ä¸ªé¡µé¢ä¸ä¸å®åå¨,åé¢ä¼å¤ç
pathName += "index.html";
hasExt = false; //æ è®°é»è®¤é¡µé¢æ¯ç¨åºèªå¨æ·»å ç
}
//è·åèµæºæ件çç¸å¯¹è·¯å¾
var filePath = path.join("http/webroot",pathName);
//è·å对åºæ件çæ档类å
var contentType = this.getContentType(filePath);
//å¦ææ件ååå¨
fs.exists(filePath,function(exists){
if(exists){
response.writeHead(200, {"content-type":contentType});
var stream = fs.createReadStream(filePath,{flags:"r",encoding:null});
stream.on("error", function() {
response.writeHead(500,{"content-type": "text/html"});
response.end("<h1>500 Server Error</h1>");
});
//è¿åæ件å
容
stream.pipe(response);
}else { //æ件åä¸åå¨çæ
åµ
if(hasExt){
//å¦æè¿ä¸ªæ件ä¸æ¯ç¨åºèªå¨æ·»å çï¼ç´æ¥è¿å404
response.writeHead(404, {"content-type": "text/html"});
response.end("<h1>404 Not Found</h1>");
}else {
//å¦ææ件æ¯ç¨åºèªå¨æ·»å çä¸ä¸åå¨ï¼å表示ç¨æ·å¸æ访é®çæ¯è¯¥ç®å½ä¸çæ件å表
var html = "<head><meta charset='utf-8'></head>";
try{
//ç¨æ·è®¿é®ç®å½
var filedir = filePath.substring(0,filePath.lastIndexOf('\\'));
//è·åç¨æ·è®¿é®è·¯å¾ä¸çæ件å表
var files = fs.readdirSync(filedir);
//å°è®¿é®è·¯å¾ä¸çæ以æ件ä¸ä¸å举åºæ¥ï¼å¹¶æ·»å è¶
é¾æ¥ï¼ä»¥ä¾¿ç¨æ·è¿ä¸æ¥è®¿é®
for(var i in files){
var filename = files[i];
html += "<div><a href='"+filename+"'>"+filename+"</a></div>";
}
}catch (e){
html += "<h1>æ¨è®¿é®çç®å½ä¸åå¨</h1>"
}
response.writeHead(200, {"content-type": "text/html"});
response.end(html);
}
}
});
},
请æ±å¤çå½æ°ä¸æå 个éç¹éè¦è¯´ä¸ä¸ï¼
对äºè·¯å¾ä¸æä¸æçï¼æµè§å¨ä¼èªå¨è¿è¡ç¼ç ï¼è±æä¸åï¼ä¸æä¼åï¼ï¼å æ¤å¨æ¥æ¶å°å°ååï¼éè¦å¯¹å°åè¿è¡è§£ç ï¼å¦åæåå¾å°çè·¯å¾åçå®è·¯å¾ä¸ç¸ç¬¦,
å½è®¿é®è·¯å¾ä¸æ¯ä»¥å
·ä½çæ件ç»å°¾ï¼å¹¶ä¸ä¸æ¯ä»¥/ç»å°¾ï¼åéè¦éè¿éå®åå ä¸/,表示å½åç®å½ï¼å¦åå½åè·¯å¾ä¸çéæèµæºä¼æ¾ä¸å°
å¦æ访é®è·¯å¾æ¯ç®å½ï¼åååºè¯¥ç®å½ä¸æææ件åæ件夹ï¼å¹¶å¯ä»¥ç¹å»è®¿é®ï¼ä¸ºäºè®©ä¸æç®å½è½æ£å¸¸æ¾ç¤ºï¼åè¿è¦å¨headerä¸è®¾ç½®charset=utf-8