无法加载资源:服务器响应状态为404(未找到)https://websitename.herokuapp.com/js/bootstrap.js

ata*_*a10 3 javascript node.js express

我正在使用express.js框架在heroku上运行node.js服务器.

这是我的服务器的样子:

var express = require('express');
var app = express();

app.use(express.static(__dirname + '/static'));
var port = process.env.PORT || 8000;

app.listen(port);
Run Code Online (Sandbox Code Playgroud)

我的index.html文件包含以下javascript链接:

<script src="/js/chartview.js"></script>
<script src="/js/bootstrap.js"></script>
<script src="/js/bootstrap-select.js"></script>
Run Code Online (Sandbox Code Playgroud)

我的目录系统如下所示:

server.js
/static
    -index.html
    -/js
        -bootstrap.js
        -bootstrap-select.js
        -chartview.js
    -/css
        -bootstrap.css
        -bootstrap-select.css
        -styles.css
Run Code Online (Sandbox Code Playgroud)

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
Run Code Online (Sandbox Code Playgroud)

当我将index.html标记更改为如下所示时,它正确加载:

<script src="chartview.js"></script>
<script src="bootstrap.js"></script>
<script src="bootstrap-select.js"></script>
Run Code Online (Sandbox Code Playgroud)

我将目录系统更改为如下所示:

server.js
/static
    -index.html
    -bootstrap.js
    -bootstrap-select.js
    -chartview.js
    -/css
        -bootstrap.css
        -bootstrap-select.css
        -styles.css
Run Code Online (Sandbox Code Playgroud)

这不是理想的,因为我想有一个/ js文件夹.

任何建议将不胜感激!

Ani*_*nha 12

从控制台错误,https://websitename.herokuapp.com/js/chartview.js该文件未在静态文件夹中检查.

因此,更改index.html以包含此类脚本.

<script src="js/chartview.js"></script>/从开始删除.因为路径是相对于index.html.

对其他三个文件执行相同的过程.

另外,您提到CSS文件已正确加载.你能说明你是如何包含那些css文件的吗?即css的路径.

在这里评论,如果这没有帮助.