【问题标题】:Failed to load resource: the server responded with a status of 404 (Not Found). Cannot link js file加载资源失败:服务器响应状态为 404(未找到)。无法链接js文件
【发布时间】:2021-03-23 13:59:01
【问题描述】:

我正在尝试在 heroku 上部署一个简单的 javascript/css/html 应用程序,但我似乎无法正确链接 js 文件。不用部署就可以正常工作,没有错误。

这是我的 index.html

<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" crossorigin="anonymous">
    <script type="text/javascript" 
        src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
    <title>Document</title>
</head>
<body class="mt-0">
    <button type="button" class="btn btn-primary ml-5 mt-5 position-fixed" style="right: 50px">Background mode</button>
    <-- more html code -->
    <script type="text/javascript" src='./js/sky-widget.js'></script>
    <script type="text/javascript" src='./js/examples.js'></script>
</body>
</html>

server.js

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

const path = require('path');

app.use(express.static(path.join(__dirname, '/pub')));

app.get('/', (req, res) => {
    res.send('<h1>This should be the root route!</h1>')
})

// Error codes
app.get('/problem', (req, res) => {
    res.status(500).send('There was a problem on the server')
});


const port = process.env.PORT || 5000
app.listen(port, () => {
    log(`Listening on port ${port}...`)
}) 

package.json

{
  "name": "weather-sky",
  "version": "1.0.0",
  "description": "Weather widget library",
  "main": "server.js",
  "scripts": {
    "start": "node server.js",
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "repository": {
    "type": "git",
  },
  "license": "ISC",
  "dependencies": {
    "express": "^4.16.4"
  }
}

我在 sky-widget.js 和 examples.js 上都收到“加载资源失败:服务器响应状态为 404(未找到)”

任何帮助将不胜感激

【问题讨论】:

    标签: javascript express heroku


    【解决方案1】:

    能否请您添加有关 example.js / sky-widget.js 的更多信息。 您也可以尝试为此更改静态文件:

    • app.use(express.static(path.join(__dirname, 'public')));

    【讨论】:

      猜你喜欢
      • 2019-09-13
      • 2015-12-01
      • 2018-08-09
      相关资源
      最近更新 更多