Express Webpack 3静态文件未在复杂路径中加载(多个/';s)
我正在使用webpack dev server ^2.5.1运行webpack 3,当我使用多个/,输入URL的路径时,浏览器会在错误的目录中查找包含我所有资产的公用文件夹。例如,当我转到localhost:8080/level1/level2时,我得到了localhost:8080/level1/styles/style.css的404,因为它在那里不存在。它应该只在localhost:8080/styles/style.css中查找。如何确保始终以正确的路径搜索我的公用文件夹 无论路径如何,我的transformed.js都能正确显示,但我的公用文件夹中的静态资产不能正确显示 这是我的网页配置文件Express Webpack 3静态文件未在复杂路径中加载(多个/';s),express,webpack,webpack-dev-server,Express,Webpack,Webpack Dev Server,我正在使用webpack dev server ^2.5.1运行webpack 3,当我使用多个/,输入URL的路径时,浏览器会在错误的目录中查找包含我所有资产的公用文件夹。例如,当我转到localhost:8080/level1/level2时,我得到了localhost:8080/level1/styles/style.css的404,因为它在那里不存在。它应该只在localhost:8080/styles/style.css中查找。如何确保始终以正确的路径搜索我的公用文件夹 无论路径如何,
var HTMLWebpackPlugin = require('html-webpack-plugin');
var HTMLWebpackPLuginConfig = new HTMLWebpackPlugin({
template: __dirname + '/app/index.html',
filename: 'index.html',
inject: 'body'
});
module.exports = {
entry: __dirname + '/app/index.js',
module: {
loaders: [
{
test: /\.js$/,
exclude: /node_modules/,
loader: 'babel-loader'
}
]
},
output: {
filename: 'scripts/transformed.js',
path: __dirname + '/build',
publicPath: '/'
},
plugins: [HTMLWebpackPLuginConfig],
devServer: {
contentBase: './public',
compress: true,
historyApiFallback: true
}
};
使用Express的server.js也会出现这种情况,只是我将其配置为返回index.html,而不是返回404。这是我的配置:
const express = require('express');
const path = require('path');
const port = process.env.PORT || 8080;
const app = express();
app.use(express.static(__dirname + '/build'));
app.use(express.static(__dirname + '/public'));
app.get('*', (req, res) => {
res.sendFile(path.resolve(__dirname, 'build/index.html'))
});
app.listen(port);
console.log("Server started");
答案实际上不是在网页包配置中,而是在我的index.html中。我必须在html标题中包含的每个静态资产中创建根路径,这意味着我必须在每个静态资产前面添加一个“/” 例如,我有
,
但是为了修复它,我做了