我目前正在建立一个使用 webpack-dev-server 和骨干路由器的站点。
直到今天,一切似乎都在工作(例如,我可以转到http://localhost:3333
我的主页和http://localhost:3333/login
登录页面。
但是今天我尝试访问我的第一个嵌套 url,http://localhost:3333/people/1
我的控制台抛出 404 GET 错误。
确切的错误是这样的:
http://localhost:3333/people/build/bundle.js 404 (Not Found)
看来我的 webpack 服务器正在尝试在错误的目录下寻找正确的文件来服务器。如果这是正确的,我不知道如何解决这个问题。我查看了其他堆栈溢出问题和 github 问题,但无济于事。有什么想法吗?
我使用 gulp 启动服务器。这是我的 gulpfile:
var gulp = require("gulp");
var webpack = require("webpack");
var server = require("webpack-dev-server");
var config = require("./webpack.config");
gulp.task("serve", function() {
new server(webpack(config), {
publicPath: config.output.publicPath,
historyApiFallback: true,
hot: true,
quiet: false,
stats: { colors: true, progress: true },
}).listen(3333, "localhost", function (err, result) {
if (err) {
console.log(err);
} else {
console.log("Listening at localhost:3333!");
}
});
});
gulp.task("default", ["serve"]);
这是我的 webpack 配置文件:
var webpack = require("webpack");
module.exports = {
entry: [
__dirname + "/app/app.js",
"webpack/hot/dev-server",
"webpack-dev-server/client?http://localhost:3333/",
],
output: {
path: __dirname + "/build",
filename: "bundle.js",
publicPath: "http://localhost:3333/",
},
plugins: [
new webpack.HotModuleReplacementPlugin(),
new webpack.NoErrorsPlugin()
],
module: {
loaders: [
{ test: /\.jsx$/, exclude: /node_modules/, loader: "react-hot-loader!babel-loader" },
{ test: /\.js$/, exclude: /node_modules/, loader: "babel-loader" },
{ test: /\.scss$/, exclude: /node_modules/, loader: "style!css!sass" },
]
},
resolve: {
root: __dirname,
extensions: ["", ".js", ".jsx"],
},
}
编辑:我刚刚发现我可以直接点击网址http://localhost:3333/#/people/1
,或者http://localhost:3333/#people/1
一切都按预期工作。
有没有一种设置可以让我不必在 url 中包含英镑符号?希望这有助于为我的问题提供更多背景信息。
跟进:我有一个全局App
组件,用于初始化骨干路由器并调用Backbone.history.start({ pushState: true })
. 这是一个代码片段。
class App {
constructor() {
console.log("starting up");
this.Router = new Router();
Backbone.history.start({ pushState: true });
}
}
module.exports = new App();
如果您注意到console.log
构造函数中的 ,这将在可以呈现的非嵌套页面上调用,但不会在不能呈现的嵌套页面上调用。