我正在尝试创建一个 Express 服务器,它使用路径参数来获取数据,然后在 SVG 中呈现。
我的问题是,当使用 REST 客户端对我的路线执行 GET 请求时,我似乎得到了缓存的响应。如果我重新启动 Node 服务器,我会得到我想要的响应:“/:data”字符串在我的 SVG 中呈现。任何后续请求都只会返回我启动 Node 服务器后提交的第一个请求参数。
如果我只是返回req.params.data
,那么每次我发出请求时它都会更新,就像我希望的那样;当在 SVG 渲染中使用它时,它不会更新 - 我的“矩形”元素保留第一个请求的值。
app.get('/render/:data'controller.render);
Controller .js
exports.render = function(req, res) {
var output = renderSVG(req.params.data);
res.send(output);
res.end();
};
function renderSVG(data) {
var svg = window.d3.select('body')
.append('div').attr('id', 'map')
.append('svg')
.attr("width", width)
.attr("height", height);
svg.append('rect').text(data);
return window.d3.select('#map').html();
}
我使用 D3 和 jsdom 在服务器端渲染 SVG。
我尝试使用中间件来确保我的响应中绝对没有缓存:
app.use(function noCache(req, res, next) {
res.setHeader('Cache-Control', 'no-cache, private, no-store, must-revalidate, max-age=0, max-stale=0, post-check=0, pre-check=0');
res.setHeader("Pragma", "no-cache");
res.setHeader("Expires", 0);
next();
});
还是没有效果。
我怀疑这与 jsdom 有关...
最佳答案
function renderSVG(data) {
var svg = window.d3.select('body')
.append('div').attr('id', 'map')
.append('svg')
.attr("width", width)
.attr("height", height);
svg.append('rect').text(data);
return window.d3.select('#map').html();
}
这看起来您总是重复使用同一个窗口。当您执行 .append('div').attr('id', 'map')
时,您总是会向该窗口添加一个带有 id 的附加 div,这将导致您的窗口具有多个div具有相同的id。在这种情况下,select by id 始终返回它找到的具有该 id 的第一个元素,导致您始终看到相同的输出。
关于javascript - Node.js jsdom/express 响应缓存?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33085760/