【问题标题】:how to add correctly script.js to html with node ERR_ABORTED 404 (Not Found)如何使用节点 ERR_ABORTED 404(未找到)将 script.js 正确添加到 html
【发布时间】:2021-07-31 20:40:29
【问题描述】:

所以我对节点完全绿色。我有带有 index.html 和 script.js 文件的基础网络。它工作正常,但现在我添加了 node.js 和正在运行它的文件 app.js。之后我的 script.js 没有正确加载到 html。除了styles.css 之外的所有文件都在同一个目录中,但是样式被正确加载到html。

抛出的错误: GET http://localhost:3000/script.js net::ERR_ABORTED 404(未找到)

index.html

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <link href="styles/styles.css">
    <script type="text/javascript" src="script.js"></script>
    <title>Document</title>
    <link rel="stylesheet" href="https://use.fontawesome.com/releases/v5.6.3/css/all.css" integrity="sha384-UHRtZLI+pbxtHCWp1t77Bi1L4ZtiqrqD80Kn4Z8NTSRyMA2Fd33n5dQ8lWUE00s/" crossorigin="anonymous">
</head>

script.js

window.addEventListener('load', function(){
    document.getElementById('sampleeditor').setAttribute('contenteditable', 'true');
});

function format(command, value) {
    document.execCommand(command, false, value);
}

app.js

const express = require('express')
const port = 3000;
const path = require('path')

const app = express()

app.get('/', function(request, response){
    response.sendFile(path.join(__dirname + '/index.html'));
})

app.listen(port)

【问题讨论】:

    标签: javascript html node.js


    【解决方案1】:

    您没有将 express 配置为提供静态文件 请参阅documentation 了解如何操作。 这是文档中的一个示例

    app.use(express.static('public'))
    app.use(express.static('files'))

    【讨论】:

      猜你喜欢
      • 2023-03-12
      • 1970-01-01
      • 2020-11-03
      • 1970-01-01
      • 2022-12-04
      • 2020-02-16
      • 1970-01-01
      • 2020-12-30
      • 1970-01-01
      相关资源
      最近更新 更多