gpt4 book ai didi

reactjs - Express.js : Set cache-control header for static assets bundled in webpack

转载 作者:行者123 更新时间:2023-12-03 13:58:56 24 4
gpt4 key购买 nike

我不知道如何使用express.js和webpack为静态资源设置Cache-Control header 。

在我的 React 应用程序中,我包含静态图像,如下所示:

 <img src={require("./images/Logo.png")} alt="Logo"/>

我的 webpack.config.js 中有一个加载程序,它将图像输出到/public/images

module: {
loaders: [
{
test: /\.png/,
loader: 'url-loader?limit=10000&name=public/images/[name].[ext]'
}]
}

最后,我将 server.js 中的/public 文件夹标记为包含静态资源,并设置 Cache-Control header

const server = express();
server.use(express.static(path.join(__dirname, 'public'), {maxAge: "30d"}));

问题是 require() 解析包含/public 文件夹的路径:

<img class="full-width" src="/public/images/Logo.png" alt="Logo">

但是 express 需要一个不带 public/ 部分的 URL。

有没有办法强制express.static()在其路径中包含/public

最佳答案

Is there a way to force express.static() to include the /public in its path?

绝对:

server.use('/public', express.static(path.join(__dirname, 'public'), {maxAge: "30d"}));

关于reactjs - Express.js : Set cache-control header for static assets bundled in webpack,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44071636/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com