【问题标题】:GET http://localhost:3000/insert.js net::ERR_ABORTED 404 (Not Found)GET http://localhost:3000/insert.js net::ERR_ABORTED 404(未找到)
【发布时间】:2021-10-16 06:06:00
【问题描述】:

我是 Web 开发的初学者,我目前正在做一个项目,但无法加载 javascript 文件 insert.js。 html 文档 upload.html 位于 public 文件夹中,而 javascript 位于主文件夹中。 我使用以下路径来链接 javascript 文件,

<script src= "../insert.js"></script>

但是好像不行,于是我试着把upload.html移到主文件夹,也就是HR,我用了下面的代码,

<script src = "/insert.js"></script>

这也不起作用,所以我尝试将 insert.js 移动到公用文件夹,并使用与我上面提到的相同的路径,但是,我无法将变量导出到 app.js 这是位于 HR 主目录中。

The image I have attached shows location of the files

我不知道是什么原因造成的,我想我也应该提到这一点,但是在 node.js 中运行我的 javascript 文件时出现错误。所以我在 node_modules/whatwg-url/dist/encoding.js 中插入了以下代码:

const {TextDecoder, TextEncoder} = require("util");

我不知道这是否与我的问题有关,但我想我会提一下。

【问题讨论】:

    标签: javascript html node.js express web-deployment


    【解决方案1】:

    localhost 只能访问你的 public 文件夹,所以你必须将 insert.js 移动到 public 文件夹中并这样做:

    <script src = "index.js"></script>
    

    此外,如果您在该 javascript 文件中有节点模块,则它不会运行,因为它无法运行模块,并且您必须为该 js 文件使用捆绑器,例如 webpack、rollup 或 snowpack。

    【讨论】:

    • 谢谢!我会研究一下,我只想将一个变量导出到服务器端。
    猜你喜欢
    • 2019-07-24
    • 2019-12-11
    • 2020-07-10
    • 2020-04-13
    • 1970-01-01
    • 2023-03-12
    • 1970-01-01
    • 2021-09-15
    • 2021-07-13
    相关资源
    最近更新 更多