javascript 如何在 Node.js 和 Jade 中使用客户端 Jquery?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24005798/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-28 01:58:26  来源:igfitidea点击:

How to use client side Jquery with Node.js and Jade?

javascriptjquerynode.jspug

提问by BigBoy1337

I am trying to put jquery code on the home page of my node.js app. This is what I have so far:

我正在尝试将 jquery 代码放在我的 node.js 应用程序的主页上。这是我到目前为止:

  script(type='text/javascript' src='public/javascripts/jquery-min.js')
  script.
    $( document ).ready(function() {
      alert('hello');

    });

which generates this html

生成这个 html

<script type="text/javascript" src="public/javascripts/jquery-min.js"></script>


<script>$( document ).ready(function() {
  alert('hello'); 

}); 

</script>

yet I get no alert when my app starts. Additionally, when I try and visit that resource directly at http://localhost:3000/public/javascripts/jquery-min.jsI get an error: Cannot GET /public/javascripts/jquery-min.js

但是当我的应用程序启动时我没有收到警报。此外,当我尝试直接访问该资源时http://localhost:3000/public/javascripts/jquery-min.js,出现错误:Cannot GET /public/javascripts/jquery-min.js

Is this expected? Is there something I have to do so that the jquery code is accessible from my app?

这是预期的吗?有什么我必须做的事情才能从我的应用程序访问 jquery 代码吗?

采纳答案by Jorge Aranda

My guess is that you haven't told your Express server where to look for static files. Add the following line to your Express configuration:

我的猜测是你没有告诉你的 Express 服务器在哪里寻找静态文件。将以下行添加到您的 Express 配置中:

// Assuming you have a line such as var app = exports.app = express();
app.use(express.static(path.join(__dirname, 'public')));

...and anything you put in the publicdirectory will be accessible through Express.

...您放入public目录的任何内容都可以通过 Express 访问。

回答by Dani?l J

After you told Express where to look for static files like this:

在您告诉 Express 在哪里查找静态文件之后:

var express = require('express');
var app = express();

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

It will look for files in the public folder, so if you want to reference any files you don't have to put "/public" in front of it. So this WON'T work:

它将在公共文件夹中查找文件,因此如果您想引用任何文件,则不必在其前面放置“/public”。所以这行不通:

script(type='text/javascript' src='public/javascripts/jquery-min.js')

But this will:

但这将:

script(type='text/javascript' src='javascripts/jquery-min.js')

For GETing the files directly it works the same, you have to omit "/public" and use it like this:

为了直接获取文件,它的工作原理相同,您必须省略“/public”并像这样使用它:

http://localhost:3000/javascripts/jquery-min.js

回答by BigBoy1337

well I am not sure why the resource isn't available. But by using script(type='text/javascript' src='http://code.jquery.com/jquery.min.js')It started working

好吧,我不确定为什么该资源不可用。但是通过使用 script(type='text/javascript' src='http://code.jquery.com/jquery.min.js')它开始工作