Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/88.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript expressJs中的堆栈jquery_Javascript_Jquery_Node.js_Express - Fatal编程技术网

Javascript expressJs中的堆栈jquery

Javascript expressJs中的堆栈jquery,javascript,jquery,node.js,express,Javascript,Jquery,Node.js,Express,我正在寻找从express app.js文件中堆叠jquery的方法。 也许这是不可能的,但我需要在app.js中使用jquery来修改html文件 例如: app.js const express=require('express')) const app=express() const$=global.jQuery=require('jQuery'); app.get('/',函数(req,res){ res.sendFile(path.join(uu dirname+'/index.htm

我正在寻找从express app.js文件中堆叠jquery的方法。
也许这是不可能的,但我需要在
app.js
中使用jquery来修改html文件

例如:

app.js

const express=require('express'))
const app=express()
const$=global.jQuery=require('jQuery');
app.get('/',函数(req,res){
res.sendFile(path.join(uu dirname+'/index.html');
$(“#点击”)。点击(功能(e){
console.log('click');
}) 
})
应用程序侦听(3000,函数(){
log('端口3000上侦听的示例应用程序!')
})
index.html


Express-Jquery
点击
也许我用错了方法…
如果有人已经这样做了

此代码属于客户端,而不是服务器端:

你应该把它放在你的
index.html
中。(或在
index.html
引用的外部脚本中)例如:

<!doctype html>
<html>
<head>
  <title>Express - Jquery</title>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
</head>
<body> 
    <button id="click" type="button">Click</button>
</body>
<script type="text/javascript">
    $("#click").click(function(e){
        console.log('click');
    });
</script>
</html>

Express-Jquery
点击
$(“#点击”)。点击(功能(e){
console.log('click');
});

服务器端代码没有浏览器中DOM的概念。它只是将页面返回给客户端,作为对它接收到的HTTP请求的响应。该页面上的客户端JavaScript与浏览器中的DOM交互。

这是不可能的。你想在这里完成什么?@goto1解释有点长,基本上,我想使用twitter API(来自app.js)并使用jquery将结果注入html文件。我建议在
app.js
中创建一个单独的路由,调用twitter API并以JSON的形式返回数据。然后在
index.html
中,您可以使用
jQuery
发出
AJAX
请求以获取该数据。另一种选择是使用模板引擎,例如
ejs
,然后使用
res.render
方法传递您需要/从Twitter API获取的数据。有效!这可能是一个解决方案。。。我会在那里询问这个评级,我就是这么想的!我在app.js中使用API,显然,我不能在html文件中使用API键。同情我再试试另一个framework@Julien:您可能正在寻找某种类型的node/express模板引擎来将数据值绑定到页面模板。另一种选择可能是将此操作完全分离。客户端页面可以向另一个服务器端路由发出单独的AJAX请求,该路由查询外部API并以JSON的形式返回数据。然后,客户端代码可以使用jQuery来填充页面中的数据,如果您设置为使用jQuery的话!我想这就是我要做的。为了进一步解释,我用electron创建了一个桌面应用程序,该应用程序分析推特流,并将结果提交给Vis.js Network。问题是我无法分发带有API密钥的软件,这就是为什么我想尝试将其与express相适应。我想你把我带上了正确的轨道!
<!doctype html>
<html>
<head>
  <title>Express - Jquery</title>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
</head>
<body> 
    <button id="click" type="button">Click</button>
</body>
<script type="text/javascript">
    $("#click").click(function(e){
        console.log('click');
    });
</script>
</html>