[英]404 error while import js file from another folders in index.html in nodejs + Ionic app
我有错误,正在尝试解决数小时。 我的文件结构:
-/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>
我不使用express
或express.static
。
从您的项目结构来看,我敢打赌它由Express.js驱动。 我还敢打赌,在快速应用程序中, www
文件夹被设置为静态文件夹,如下所示:
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
文件夹中,它就可以正常工作。
这里的问题是您尝试从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>
您正在尝试从错误的目录加载文件。 您要么必须使用以下路径:
src="../node_modules/
或这个:
src="/node_modules"
如果上面的目录是您的根目录。
还令人困惑的是,您在其中注释了该错误的行不是该错误实际来自的行。 为什么要尝试两次链接到文件?
//编辑:由于Romain比我快一分钟,他应该得到正确的答案。我会说:P仅当它确实解决了您的问题时:D
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.