将本地脚本文件添加到EJS视图 [英] Adding local script files to EJS views

查看:81
本文介绍了将本地脚本文件添加到EJS视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Node / Express / EJS应用。它有一个用于查看的文件夹和一个用于客户端文件的文件夹。后者还有一个用于javascript的文件夹,其中有一个名为frontend.js的文件。

I've got a Node/Express/EJS app. It's got a folder for views and another for client files. The latter has another folder for javascript, where I have a file called frontend.js.

我想在此视图中加载jQuery和frontend.js文件。

I'd like to load jQuery and the frontend.js file in this view.

标准脚本标记无效。

    <script src="//ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
    <script type="text/javascript" src="../client/js/frontend.js"></script>

jQuery加载后,frontend.js抛出404错误。

jQuery loads, frontend.js throws a 404 error.

是否有一种我不知道的特殊方法?

Is there a special way of doing this that I'm not aware of?

推荐答案

c $ c> app.js 文件添加以下内容:

in your app.js file add this :

app.use(express.static(path.join(__dirname, 'client')))

将第二个脚本标签更改为:

change the second script tag to :

<script type="text/javascript" src="/js/frontend.js"></script>

我假设您的 app.js 文件和 client 文件夹位于同一文件夹中。

I'm assuming that your app.js file and client folder are in the same folder.

这篇关于将本地脚本文件添加到EJS视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆