简体   繁体   English

从Node.js + Ionic应用程序的index.html中的其他文件夹导入js文件时出现404错误

[英]404 error while import js file from another folders in index.html in nodejs + Ionic app

I have error and am trying to solve it for hours. 我有错误,正在尝试解决数小时。 My file structure: 我的文件结构:

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

in index.html I have such code 在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>

Problem is bundle.js is included fine, but ng-cordova.min.js gives 404 error 问题是bundle.js被包括在内,但是ng-cordova.min.js给出了404错误

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

Edit : I also tried this without success 编辑 :我也尝试了此没有成功

<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>

I don't use express or express.static . 我不使用expressexpress.static

Judging by your project structure I'm betting that it is powered by Express.js . 从您的项目结构来看,我敢打赌它由Express.js驱动。 I'm also betting that the www folder was set as a static folder in the express app like this: 我还敢打赌,在快速应用程序中, www文件夹被设置为静态文件夹,如下所示:

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

In which case your server will only serve files located in the www folder. 在这种情况下,您的服务器将仅提供www文件夹中的文件。 You need to include any client dependencies in the www folder: 您需要在www文件夹中包括所有客户端依赖项:

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

And then load them like so: 然后像这样加载它们:

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

UPDATE 更新

Simply put node_modules inside the www folder, it should work fine. 只需将node_modules放在www文件夹中,它就可以正常工作。

The problem here is that you try to load your ng-cordova.min.js file from the index.html folder. 这里的问题是您尝试从index.html文件夹加载ng-cordova.min.js文件。 You should change: 您应该更改:

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

For this: 为了这:

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

You are trying to load the file from the wrong directory. 您正在尝试从错误的目录加载文件。 You either have to use this path: 您要么必须使用以下路径:

src="../node_modules/

or this: 或这个:

src="/node_modules"

in case the above directory is your root directory. 如果上面的目录是您的根目录。

What's also confusing is that the line you commented the error in is not the line the error actually comes from. 还令人困惑的是,您在其中注释了该错误的行不是该错误实际来自的行。 Why are you trying to link to the file twice? 为什么要尝试两次链接到文件?

//Edit: As Romain was a minute faster than me he deserves the right answer reward I'd say :P Only if it actually solves your problem ofc :D //编辑:由于Romain比我快一分钟,他应该得到正确的答案。我会说:P仅当它确实解决了您的问题时:D

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM