我正在使用express.static
来服务一些静态文件。我想在将文件返回给客户端之前修改一些文件内容。下面是/public
路径下静态资源的源代码。
app.use('/public', express.static(path.join(__dirname, 'public')))
在公共目录下有一些html文件,我想在响应客户端之前修改这些html文件。如何使用static完成此操作?我知道可以添加自定义中间件,但不确定如何操作。是否有我可以使用的中间件模式?
express.static()
来修改文件,因为这不是它的作用。app.get("/public/somefile", function(req, res) {
fs.readFile(path.join(__dirname, 'public', 'somefile'), function(err, data) {
if (err) {
res.sendStatus(404);
} else {
// modify the data here, then send it
res.send(data);
}
});
});
// put express.static after your other routes that serve from the public
// directory so those other routes are matched first
app.use('/public', express.static(path.join(__dirname, 'public')));