上記のcnodejs.org上の静的サーバーの例を参照して、キャッシュと圧縮を含む次のnodejs staticサーバーの例を書きました。コードは次のとおりです。
コードコピーは次のとおりです。
/**
*静的ファイルサーバーテストの例
*ユーザー:xuwm
*日付:13-5-17
*時間:午前8時38分
*このテンプレートを変更するには、ファイルを使用します|設定|ファイルテンプレート。
*/
var port = 3333;
var http = require( "http");
var url = require( "url");
var fs = require( "fs");
var path = require( "path");
var mime = require( "./ mime")。型;
var config = require( "./ config");
var zlib = require( "zlib");
// HTTPサーバーを作成します
var server = http.createserver(function(request、response){
var obj = url.parse(request.url);
Response.setheader( "server"、 "node/v8");
console.log(obj);
var pathname = obj.pathname;
if(pathname.slice(-1)=== "/"){
pathname = pathname+config.welcome.file; //デフォルトのindex.htmlは、現在のデフォルトで選択されます
}
var realpath = path.join( "assets"、path.normalize(pathname.replace(//././ g、 "")));
console.log(RealPath);
var pathhandle = function(realpath){
// fs.statメソッドを使用してファイルを取得します
fs.stat(realpath、function(err、stats){
if(err){
Response.writehead(404、 "Not ing"、{'content-type': 'text/plain'});
respons.write( "request"+realpath+"は見つかりません");
Response.End();
}それ以外{
if(stats.isdirectory()){
}それ以外{
var ext = path.extname(realpath);
ext = ext? ext.slice(1): 'unknown';
var contentType = mime [ext] || 「テキスト/プレーン」;
Response.setheader( "content-type"、contentType);
var lastModified = stats.mtime.toutcstring();
var ifModifiedsince = "if-modified-since" .tolowercase();
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 [ifModifiedsince] && lastModified == request.headers [ifModifiedsince]){
console.log( "ブラウザキャッシュからのフェッチ")
Response.Writehead(304、 "Modified");
Response.End();
} それ以外 {
var raw = fseatereadStream(RealPath);
var Acceptencoding = request.headers ['Accept-Encoding'] || "";
var matched = ext.match(config.compress.match);
if(matched && acceptencoding.match(// bgzip/b/)){
Response.Writehead(200、 "ok"、{'content-encoding': 'gzip'});
raw.pipe(zlib.creategzip())。pipe(response);
} else if(matched && acceptencoding.match(// bdeflate/b/)){
respons.writehead(200、 "ok"、{'content-encoding': 'deflate'});
raw.pipe(zlib.createdeflate())。pipe(response);
} それ以外 {
Response.Writehead(200、 "ok");
raw.pipe(response);
}
}
}
}
});
}
PathHandle(RealPath);
});
server.listen(port);
console.log( "httpサーバーがポートで実行されます:"+ポート);
まず、JSファイルにAssetsフォルダーを作成し、index.html、demo.jsなど、閲覧する静的ファイルを配置する必要があります。
実行方法は次のとおりです。コマンドラインの上記のJSファイルディレクトリに切り替えてから、ノードJSファイル名を入力します
ブラウザにhttp:// localhost:3333/を入力して、効果を確認します。
- 上記のコードの2つの欠落しているモジュールに入力
mime.js
コードコピーは次のとおりです。
exports.types = {
「CSS」:「テキスト/CSS」、
「GIF」:「Image/GIF」、
「HTML」:「Text/HTML」、
「ICO」:「Image/X-Icon」、
「jpeg」:「image/jpeg」、
「jpg」:「image/jpeg」、
「JS」:「Text/JavaScript」、
「Json」:「Application/Json」、
「PDF」:「アプリケーション/PDF」、
「PNG」:「Image/PNG」、
「svg」:「image/svg+xml "、
「SWF」:「Application/X-ShockWave-Flash "、
「TIFF」:「Image/Tiff」、
「txt」:「テキスト/プレーン」、
「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、
最大:60 * 60 * 24 * 365
};
exports.compress = {
一致: /css | js | html /ig
};
exports.welcome = {
ファイル:「index.html」
};