筆記
var http=require("http");//引入http模塊
var fs=require("fs");//引入文件讀寫模塊
var url=require("url");//引入url處理模塊
var server=new http.Server();//實(shí)例化一個(gè)服務(wù)器對(duì)象
//監(jiān)聽請求事件
server.on("request",(req,res)=>{
var pathname=url.parse(req.url).pathname;//請求路徑
switch(pathname){//路徑分析。根據(jù)不同的路徑返回不同的資源
case ""||"/":
}
});
作業(yè):完善上的代碼,實(shí)現(xiàn)一個(gè)靜態(tài)資源服務(wù)器叫惊。
作用
完善課上的代碼實(shí)現(xiàn)一個(gè)靜態(tài)資源服務(wù)器。
創(chuàng)建三個(gè)文件:程序入口文件app.js做修、資源映射文件mime.js霍狰、配置文件config.js
程序入口文件app.js
var http = require("http");
var url = require("url");
var fs = require("fs");
var path = require("path");
var zlib = require("zlib");//壓縮模塊
var mime = require("./mime").types;
var config = require("./config");
var server = new http.Server();
server.on("request", function (request, response) {//監(jiān)聽請求事件
var pathname = url.parse(request.url).pathname;
switch(pathname){//路由控制;
case "/"||"index":
pathname="/index.html";
break;
}
var realPath = path.join("assets", path.normalize(pathname));
var ext = path.extname(realPath);//解析文件的后綴名饰及;
if(ext){//不處理沒有后綴名的realPath,這一點(diǎn)交給路由控制蔗坯;
fs.stat(realPath, function (err, stat) {//讀取文件屬性
if (err) {//如果沒找到文件返回404
response.writeHead(404, "Not Found", {'Content-Type': 'text/plain'});
response.write("NOT FOUND on this server");
response.end();
} else {
ext = ext.slice(1);
var contentType = mime[ext] || "text/plain";
response.setHeader('Content-Type', contentType);
var lastModified = stat.mtime.toUTCString();//文件的最后修改時(shí)間
response.setHeader("Last-Modified", lastModified);
if (ext.match(config.Expires.fileMatch)) {//緩存
var expires = new Date();
expires.setTime(expires.getTime() + config.Expires.maxAge * 1000);
response.setHeader("Expires", expires.toUTCString());
response.setHeader("Cache-Control", "max-age=" + config.Expires.maxAge);
}
if (request.headers["if-modified-since"] && lastModified == request.headers["if-modified-since"]) {//判斷文件是否修改過
response.writeHead(304, "Not Modified");
response.end();
} else {如果修改過或者第一次傳輸則返回文件
var raw = fs.createReadStream(realPath);
var ae = request.headers["accept-encoding"] || "";//獲取瀏覽器支持的壓縮格式
var matched = ext.match(config.Compress.match);
if (matched && ae.match(/\bgzip\b/)) {
response.writeHead(200, "ok", {"Content-Encoding": "gzip"});
raw.pipe(zlib.createGzip()).pipe(response);
} else if (matched && ae.match(/\bdeflate\b/)) {
response.writeHead(200, "ok", {"Content-Encoding": "deflate"});
raw.pipe(zlib.createDeflate()).pipe(response);
} else {//如果不支持壓縮,則直接傳輸
response.writeHead(200, "ok");
raw.pipe(response);
}
}
}
});
}else{//如果沒有后綴名燎含,則直接斷開連接
response.end();
}
});
server.listen(3000);
資源映射文件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"
};
配置文件config.js
exports.Expires = {//緩存
fileMatch: /^(gif|png|jpg|js|css)$/ig,//需要緩存的文件類型
maxAge: 60*60*24*365//到期時(shí)間
};
exports.Compress={//需要壓縮傳輸?shù)奈募愋? match:/css|js|html/ig
};