อ้างถึงตัวอย่างเซิร์ฟเวอร์คงที่ด้านบน cnodejs.org ฉันเขียนตัวอย่างเซิร์ฟเวอร์ NodeJS คงที่ต่อไปนี้ซึ่งมีแคชและการบีบอัดและรหัสมีดังนี้:
การคัดลอกรหัสมีดังนี้:
-
* ตัวอย่างการทดสอบเซิร์ฟเวอร์ไฟล์แบบคงที่
* ผู้ใช้: xuwm
* วันที่: 13-5-17
* เวลา: 8:38 น
* หากต้องการเปลี่ยนเทมเพลตนี้ใช้ไฟล์ | การตั้งค่า เทมเพลตไฟล์
-
var port = 3333;
var http = ต้องการ ("http");
var url = ต้องการ ("url");
var fs = ต้องการ ("fs");
var path = ต้องการ ("เส้นทาง");
var mime = ต้องการ ("./ mime"). ประเภท;
var config = ต้องการ ("./ config");
var zlib = ต้องการ ("zlib");
// สร้างเซิร์ฟเวอร์ HTTP
var server = http.createServer (ฟังก์ชั่น (คำขอ, การตอบกลับ) {
var obj = url.parse (request.url);
Response.Setheader ("เซิร์ฟเวอร์", "Node/V8");
console.log (obj);
var pathname = obj.pathName;
if (pathname.slice (-1) === "/") {
pathName = pathName+config.welcome.file; // index.html เริ่มต้นถูกเลือกภายใต้ค่าเริ่มต้นปัจจุบัน
-
var realpath = path.join ("สินทรัพย์", path.normalize (pathname.replace (//././ g, ""));
console.log (RealPath);
var pathhandle = function (realpath) {
// ใช้วิธี fs.stat เพื่อรับไฟล์
fs.stat (realpath, ฟังก์ชั่น (err, stats) {
ถ้า (err) {
Response.writehead (404, "ไม่พบ", {'เนื้อหาประเภท': 'ข้อความ/ธรรมดา'});
Response.write ("คำขอ"+RealPath+"ไม่พบ");
Response.end ();
}อื่น{
if (stats.isdirectory ()) {
}อื่น{
var ext = path.extName (RealPath);
ext = ext? ext.slice (1): 'ไม่ทราบ';
var contentType = mime [ext] || "ข้อความ/ธรรมดา";
Response.Setheader ("ประเภทเนื้อหา", contentType);
var lastModified = stats.mtime.toutcstring ();
var ifmodifiedSince = "if-modified-since" .toLowercase ();
Response.SetheAder ("Modified ล่าสุด", LastModified);
if (ext.match (config.expires.filematch)) {
var expires = date ใหม่ ();
Expires.Settime (Expires.getTime () + config.expires.maxage * 1000);
Response.SetheAder ("หมดอายุ", Expires.toutcString ());
Response.Setheader ("Cache-Control", "Max-age =" + config.expires.maxage);
-
if (request.headers [ifmodifiedsince] && lastModified == request.headers [ifmodifiedsince]) {
console.log ("ดึงข้อมูลจากเบราว์เซอร์แคช")
Response.writehead (304, "ไม่ได้แก้ไข");
Response.end ();
} อื่น {
var raw = fs.createReadstream (realpath);
var acceptencoding = request.headers ['Accept-encoding'] || -
var matched = ext.match (config.compress.match);
if (จับคู่ && acceptencoding.match (// bgzip/b/)) {
Response.writehead (200, "OK", {'การเข้ารหัสเนื้อหา': 'GZIP'});
Raw.pipe (zlib.creategzip ()). ท่อ (ตอบสนอง);
} else ถ้า (จับคู่ && acceptencoding.match (// bdeflate/b/)) {
Response.writehead (200, "OK", {'การเข้ารหัสเนื้อหา': 'deflate'});
Raw.pipe (zlib.createdeflate ()). ท่อ (ตอบสนอง);
} อื่น {
Response.writehead (200, "OK");
Raw.pipe (การตอบสนอง);
-
-
-
-
-
-
Pathhandle (RealPath);
-
Server.Listen (พอร์ต);
console.log ("เซิร์ฟเวอร์ HTTP เรียกใช้ในพอร์ต:"+พอร์ต);
ก่อนอื่นคุณต้องสร้างโฟลเดอร์สินทรัพย์ในไฟล์ JS และวางไฟล์คงที่ที่คุณต้องการเรียกดูเช่น index.html, demo.js ฯลฯ
วิธีการรันคือ: สลับไปที่ไดเรกทอรีไฟล์ JS ด้านบนบนบรรทัดคำสั่งจากนั้นป้อนชื่อไฟล์โหนด JS
ป้อน http: // localhost: 3333/ในเบราว์เซอร์เพื่อดูเอฟเฟกต์
-เติมในสองโมดูลที่หายไปในรหัสข้างต้น
mime.js
การคัดลอกรหัสมีดังนี้:
exports.types = {
"CSS": "ข้อความ/CSS",
"gif": "image/gif",
"html": "ข้อความ/html"
"ICO": "Image/X-ICON",
"jpeg": "image/jpeg",
"jpg": "image/jpeg",
"js": "ข้อความ/จาวาสคริปต์",
"JSON": "Application/JSON"
"PDF": "Application/PDF"
"png": "image/png"
"SVG": "Image/SVG+XML"
"swf": "แอปพลิเคชัน/x-shockwave-flash"
"TIFF": "Image/TIFF",
"txt": "ข้อความ/ธรรมดา"
"WAV": "Audio/X-WAV"
"WMA": "Audio/X-MS-WMA"
"WMV": "Video/X-MS-WMV"
"xml": "ข้อความ/xml"
-
config.js
การคัดลอกรหัสมีดังนี้:
Exports.expires = {
fileMatch: /^(gif | png | jpg | js | css) $ /ig,
Maxage: 60 * 60 * 24 * 365
-
Exports.Compress = {
Match: /CSS | JS | HTML /IG
-
exports.welcome = {
ไฟล์: "index.html"
-