简体   繁体   English

如何在前端加载本地脚本?

[英]How to load local scripts on the front-end?

CODE: 码:

app.js app.js

//Static Folder
app.use(express.static(path.join(__dirname, "/public")));

section1/index.ejs section1 / index.ejs

<script src="/public/js/firebase.js"></script>
<script src="/public/js/angular.js"></script>
<script src="/public/js/angularfire.js"></script>
<script src="/public/js/jquery-3.1.1.min.js"></script>

PROBLEM: 问题:

Error 404 for all those files when I load section1/index.ejs 当我加载section1 / index.ejs时,所有这些文件的错误404

What is missing ? 什么东西少了 ? What mistake have I made ? 我犯了什么错误?

You need to pass a first argument to app.use , which will be the public path. 您需要将第一个参数传递给app.use ,这将是公共路径。

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

Then on the client: 然后在客户端上:

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

You can also use /public instead of / . 您也可以使用/public代替/

Try using: 尝试使用:

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

And I think if you want to use it as a middleware without a virtual path, you should use it like this: 而且我认为,如果您想将其用作没有虚拟路径的中间件,则应该这样使用:

app.use(express.static('public'));

Without defining the whole path, Express will get the folder for you automatically. 在不定义整个路径的情况下,Express会自动为您获取文件夹。

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

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