Javascript 加载资源失败:服务器响应状态为 404(未找到) https://websitename.herokuapp.com/js/bootstrap.js
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/27435837/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Failed to load resource: the server responded with a status of 404 (Not Found) https://websitename.herokuapp.com/js/bootstrap.js
提问by atasca10
I'm running a node.js server on heroku using the express.js framework.
我正在使用 express.js 框架在 heroku 上运行 node.js 服务器。
Here is what my server looks like:
这是我的服务器的样子:
var express = require('express');
var app = express();
app.use(express.static(__dirname + '/static'));
var port = process.env.PORT || 8000;
app.listen(port);
My index.html file has the following javascript links:
我的 index.html 文件有以下 javascript 链接:
<script src="/js/chartview.js"></script>
<script src="/js/bootstrap.js"></script>
<script src="/js/bootstrap-select.js"></script>
My directory system looks like this:
我的目录系统如下所示:
server.js
/static
-index.html
-/js
-bootstrap.js
-bootstrap-select.js
-chartview.js
-/css
-bootstrap.css
-bootstrap-select.css
-styles.css
Chrome displays my html page with the appropriate css styles but the console says:
Chrome 使用适当的 css 样式显示我的 html 页面,但控制台显示:
Failed to load resource: the server responded with a status of 404 (Not Found) https://websitename.herokuapp.com/js/chartview.js
Failed to load resource: the server responded with a status of 404 (Not Found) https://websitename.herokuapp.com/js/bootstrap.js
Failed to load resource: the server responded with a status of 404 (Not Found) https://websitename.herokuapp.com/js/bootstrap-select.js
It loads properly when I change my index.html tags to look like this:
当我将 index.html 标签更改为如下所示时,它会正确加载:
<script src="chartview.js"></script>
<script src="bootstrap.js"></script>
<script src="bootstrap-select.js"></script>
And I change my directory systems to look like this:
我将目录系统更改为如下所示:
server.js
/static
-index.html
-bootstrap.js
-bootstrap-select.js
-chartview.js
-/css
-bootstrap.css
-bootstrap-select.css
-styles.css
This is not Ideal, as I would like to have a /js folder.
这不是理想的,因为我想要一个 /js 文件夹。
Any suggestions will be greatly appreciated!
任何建议将不胜感激!
回答by Aniket Sinha
From the console error, https://websitename.herokuapp.com/js/chartview.js, the file is not being checked in static folder.
根据控制台错误,https://websitename.herokuapp.com/js/chartview.js该文件未在静态文件夹中检查。
So, change your index.html to include scripts like this.
因此,更改您的 index.html 以包含这样的脚本。
<script src="js/chartview.js"></script>i.e remove /from beginning. as path is relative to index.html.
<script src="js/chartview.js"></script>即/从头删除。因为路径是相对于 index.html 的。
Follow same process for other three files.
对其他三个文件执行相同的过程。
Also, you mentioned that the CSS files are loaded correctly. Can you show how exactly are you including those css files? i.e the path for css.
此外,您提到 CSS 文件已正确加载。你能展示一下你是如何包含这些 css 文件的吗?即 css 的路径。
Comment here, if that doesn't help.
如果这没有帮助,请在此处发表评论。
回答by MacInnis
Try Placing your files in the public folder and use the path like this""
尝试将您的文件放在公共文件夹中并使用这样的路径""
回答by Arthur Zhang
Don't use "__dirname" Only using
不要使用“__dirname” 只使用
app.use(express.static('static'));

