【问题标题】:404 error while import js file from another folders in index.html in nodejs + Ionic app从nodejs + Ionic应用程序中index.html中的另一个文件夹导入js文件时出现404错误
【发布时间】:2015-12-11 13:56:23
【问题描述】:

我有错误,并试图解决它几个小时。 我的文件结构:

-/node_modules
-/www
---bundle.js
---index.html

在 index.html 我有这样的代码

<script src="node_modules/ng-cordova/dist/ng-cordova.min.js" type="text/javascript"></script>

<!-- cordova script (this will be a 404 during development) -->
<script src="cordova.js"></script>

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

问题是 bundle.js 包含在内,但 ng-cordova.min.js 给出 404 错误

Cannot GET /node_modules/ng-cordova/dist/ng-cordova.min.js

编辑:我也试过了,但没有成功

<script src="/node_modules/ng-cordova/dist/ng-cordova.min.js" type="text/javascript"></script>
<script src="../node_modules/ng-cordova/dist/ng-cordova.min.js" type="text/javascript"></script>

我不使用expressexpress.static

【问题讨论】:

    标签: javascript angularjs node.js cordova ionic


    【解决方案1】:

    从您的项目结构来看,我敢打赌它由Express.js 提供支持。我还打赌 www 文件夹在 express 应用程序中被设置为静态文件夹,如下所示:

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

    在这种情况下,您的服务器将仅提供位于 www 文件夹中的文件。您需要在 www 文件夹中包含所有客户端依赖项:

    -/node_modules/
    -/www/
    --lib/
    ---ng-cordova/
    --bundle.js
    --index.html
    

    然后像这样加载它们:

    <script src="lib/ng-cordova/dist/ng-cordova.min.js" type="text/javascript"></script>
    

    更新

    只需将 node_modules 放在 www 文件夹中,它应该可以正常工作。

    【讨论】:

    • 我认为express.js是你应该直接使用的东西,我没有使用它所以我没有考虑这个方向。
    • 但是当我在我的app.js 中使用require 模块时,它工作正常。为什么会这样?
    • 啊,对不起。刚刚更新了我的答案。我经常使用 express,所以当我看到 node_modules 文件夹时,我只是假设您也在使用它。只需将 node_modules 放在 www 文件夹中,它应该可以正常工作。
    • 我有点困惑,因为我使用了npm,它创建了文件夹node_modules,所以我不能简单地移动它。而且我认为node_modules 现在来对地方了。
    • 您可以简单地在www 文件夹中运行npm install。根据我从您的代码和项目结构中收集到的信息,您的 http 服务器仅提供来自 www 目录的静态文件。如果我的假设有误,请纠正我。
    【解决方案2】:

    这里的问题是您尝试从 index.html 加载您的 ng-cordova.min.js 文件 文件夹。 你应该改变:

    <script src="node_modules/ng-cordova/dist/ng-cordova.min.js" type="text/javascript"></script>
    

    为此:

    <script src="../node_modules/ng-cordova/dist/ng-cordova.min.js" type="text/javascript"></script>
    

    【讨论】:

      【解决方案3】:

      您正试图从错误的目录加载文件。 您要么必须使用此路径:

      src="../node_modules/
      

      或者这个:

      src="/node_modules"
      

      如果上面的目录是你的根目录。

      同样令人困惑的是,您在其中注释错误的行并不是错误实际来自的行。为什么要两次尝试链接到该文件?

      //编辑:由于 Romain 比我快一分钟,他应该得到正确的答案奖励我会说:P 只有当它真正解决了你的问题时:D

      【讨论】:

      • 您是否安装了任何其他可以测试从中加载 js 文件的节点模块?
      • 是的,我还有其他几个模块,没有人以这种方式加载。我为他们使用browserify,这个模块也适用于browserify
      • 很抱歉没有得到您的答案,但其他模块是否正在加载?
      • 我测试了其他模块,但它们没有加载到 index.html 中。同样的错误。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-02-14
      • 1970-01-01
      • 2012-09-07
      • 1970-01-01
      • 1970-01-01
      • 2021-11-03
      • 1970-01-01
      相关资源
      最近更新 更多