0

動的な node.js Web サイトを作成しています。ブラウザにページを保存させたくありません。つまり、ユーザーがリロードをクリックするたびに、ページを最初からリロードしたいのです。現時点では、"Cache-Control":"no store" を送信しているにもかかわらず、ページがまだキャッシュされているように見えます。ここに私のサーバーがあります:

// requires node's http module
var http=require('http');
var url=require('url');
var fs=require('fs');
// creates a new httpServer instance
http.createServer(function (req, res) {
// this is the callback, or request handler for the httpServer
log('in server callback')
res.ins=res.write;

var parse=url.parse(req.url,true);
var path0=parse.pathname;
var mime=core.getMime(path0)
console.log(path0)
// respond to the browser, write some headers so the 
// browser knows what type of content we are sending

var servePage=function(){
    var path='./page'+path0+'.js'
    console.log(path)
    fs.exists(path,function(e){
        if(e){
            log('serving page')
            var exp=require(path); 
            if(exp && exp.html){
                var html=exp.html
            }
            else{
                var html='bad file'
            }
        }
        else{
            console.log('no page to serve')
            var exp=require('./page/pageHome.js')
            var html=exp.html
        }
        res.writeHead(200, {'Content-Type': mime, 'Cache-Control': 'no store'});
        res.ins(html);
        res.end();
    })
}
servePage()
}).listen(8080); // the server will listen on port 8080

また、「 http://mydomain.com/page?q=42 」などのランダムなクエリ文字列を使用してセルフ リンクを作成しようとしましたが、それでもキャッシュをバイパスしませんでした。私は何を間違っていますか?ありがとう!

4

1 に答える 1

1

あなたはそれを一般的に間違っています。

をお読みくださいrequire。node.js のモジュールをロードして実行するために使用されます。http 経由のリクエストに対して実際にファイルを提供するためのものではありません。
requireは常に実行結果をキャッシュし、モジュールの参照を保持します。手動でクリアする必要がありますが、上記のように、これはあなたの場合ではありません。

node.js経由でファイルを送信する方法について説明している、この素晴らしい投稿をお読みください: Nodejs は応答でファイルを送信します。

キャッシュなしヘッダーも設定できますが、これは「悪い方法」です。ヘッダーにはまったく触れず、フロントエンドで追加のクエリを実行することをお勧めしますが、常にではありません。

res.header('Cache-Control', 'no-cache, private, no-store, must-revalidate, max-stale=0, post-check=0, pre-check=0');
于 2013-07-09T17:32:12.513 に答える