简体   繁体   English

在父目录中使用javascript不起作用

[英]Using javascript in parent directory does not work

I have recently started to try angularjs, my website works fine when I have my index.html and angular.min.js in the same directory: 我最近开始尝试angularjs,当我将index.html和angular.min.js放在同一目录中时,我的网站可以正常工作:

<!--at the end of the <body> of index.html-->
<script src="angular.min.js"></script>

However, I already have a copy of this script in ../node_modules/angular/angular.min.js 但是,我已经在../node_modules/angular/angular.min.js拥有此脚本的副本。

I want to use this script instead: 我想改用以下脚本:

<script src="../node_modules/angular/angular.min.js"></script>

This just does not work. 这只是行不通的。

I have also tried to move this to a child directory like 我也尝试将其移动到子目录,例如

<script src="node/angular.min.js"></script>

This worked. 这工作了。 Hence, the problem is really why I cannot load script from parent directories? 因此,问题实际上就是为什么我无法从父目录加载脚本?

FYI, my tree looks like: 仅供参考,我的树看起来像:

project/
  node_modules/
    angular/
      angular.min.js
  app1/
    index.html

In node_modules you theoretically can have some important modules, data, that should be hidden from outside. 理论上,在node_modules中,您可以具有一些重要的模块,即数据,应该从外部隐藏它们。 If you expose all node_modules directory then anybody can access all your files from there. 如果公开所有node_modules目录,那么任何人都可以从那里访问所有文件。

But we can expose only angular directory (If you need you can expose more file or even full node_modules but this is not the best idea). 但是我们只能公开角度目录(如果需要,可以公开更多文件,甚至可以公开完整的node_modules,但这不是最好的主意)。 In your server file add: 在您的服务器文件中添加:

app.use('/angular', express.static(__dirname + '/node_modules/angular/'));

And now you in frontend you can get angular: 现在,您在前端就可以了解更多信息:

<script src="angular/angular.min.js"></script>

Edit: This works only if you use express in node.js (if yes then please add express tag to your question) 编辑:仅当您在node.js中使用express时,此方法才有效(如果是,请在您的问题中添加express标签)

Edit2 (answer OP question from comemnt): Node.js server and express by default doesn't serve static files. Edit2(来自comemnt的OP回答问题):默认情况下,Node.js服务器和express不提供静态文件。

should just be another directory in terms of HTML 就HTML而言应该只是另一个目录

No. If you have correctly configured server, you can't just open any file on this server from browser (this is serious security issue). 否。如果您已正确配置服务器,则不能仅通过浏览器打开此服务器上的任何文件(这是严重的安全问题)。

If you have not configured 'serving static files' in your node.js server but see static files in some directories, then it is possible that you serve them by apache or nginx server. 如果您尚未在node.js服务器中配置“服务静态文件”,但在某些目录中看到了静态文件,则有可能由apache或nginx服务器提供它们。 You can try to change their configuration instead. 您可以尝试更改其配置。

If you don't have express only pure node.js then serving static files is more complicated, please look at this gist : https://gist.github.com/ryanflorence/701407 如果您不只表达纯的node.js,则提供静态文件会更加复杂,请查看以下要点: https : //gist.github.com/ryanflorence/701407

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

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