gpt4 book ai didi

backbone.js - Webpack 开发服务器无法加载嵌套 url(GET 404 错误)

转载 作者:行者123 更新时间:2023-12-04 17:08:49 29 4
gpt4 key购买 nike

我目前正在建立一个使用 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/1http://localhost:3333/#people/1一切都按预期进行。

是否有设置可以让我不必在 url 中包含英镑符号?希望这有助于为我的问题提供更多背景信息。

跟进:我有全局 App初始化 Backbone 路由器并调用 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在构造函数中,这在可以渲染的非嵌套页面上调用,但在不能渲染的嵌套页面上调用。

最佳答案

我终于找到了我的问题的答案。

我在每个页面上加载的 index.html 文件以前看起来像这样:

<!DOCTYPE html>
<html>
<head>
...
</head>
<body>
<script type="text/javascript" src="./build/bundle.js" charset="utf-8"></script>
</body>
</html>

因为脚本的源是相对路径,所以在嵌套的 url 上找不到脚本。

将源更改为此修复了它:
<script type="text/javascript" src="http://localhost:3333/build/bundle.js" charset="utf-8"></script>

关于backbone.js - Webpack 开发服务器无法加载嵌套 url(GET 404 错误),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30011023/

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