[英]Using jQuery in Node.js
Hi I am new here and hope that I can make myself clear.嗨,我是新来的,希望我能说清楚。 I've been trying to call(?) jQuery in node.js, but everything I do results a failure.
我一直在尝试在 node.js 中调用 (?) jQuery,但我所做的一切都失败了。 I've been trying to solve this for hours and still stuck hopefully there is someone who can help me with this !
我一直试图解决这个问题几个小时,但仍然坚持希望有人可以帮助我解决这个问题!
I am trying to fetch some info from twitch using their api.我正在尝试使用他们的 api 从 twitch 获取一些信息。 In this case if a streamer is online or offline.
在这种情况下,如果主播在线或离线。 When I open the page localy ( without node.js) the jQuery code works fine.
当我打开本地页面(没有 node.js)时,jQuery 代码工作正常。 However when I try to use node.js it gives errors on this: $.
但是,当我尝试使用 node.js 时,它会出现以下错误:
This is after some research and random trying.. (still new to node.js and jQuery):这是经过一些研究和随机尝试..(对 node.js 和 jQuery 来说仍然是新的):
` http://imgur.com/a/2nhho ` http://imgur.com/a/2nhho
` `
Edit: The reason why I am trying to use node.js is to update my website without refreshing.编辑:我尝试使用 node.js 的原因是在不刷新的情况下更新我的网站。 So If someone goes online/offline, he/she should not be forced to refresh.
所以如果有人上线/下线,他/她不应该被迫刷新。
"When the only tool you have is a hammer, every problem looks like a nail." “当你拥有的唯一工具是锤子时,每个问题看起来都像钉子。”
In this case, you're trying to use the wrong tool for the job.在这种情况下,您正在尝试使用错误的工具来完成这项工作。 jQuery is built and optimized specifically for use in a browser environment.
jQuery 专为在浏览器环境中使用而构建和优化。 While you could force it to work by having a shadow DOM... don't.
虽然您可以通过使用 shadow DOM 来强制它工作……但不要这样做。
If you wanted to use jQuery's selectors and DOM manipulation features, cheerio
basically implements that API, but specifically for Node.js/server-side.如果您想使用 jQuery 的选择器和 DOM 操作功能,
cheerio
基本上实现了该 API,但专门用于 Node.js/服务器端。
However, you sound like you want to use it's ajax()
method (or one of it's helpers like post()
or get()
.但是,您听起来像是想使用它的
ajax()
方法(或者它的一个助手,如post()
或get()
。
Instead, you should use something built for Node.相反,您应该使用为 Node.js 构建的东西。
node-fetch
( https://www.npmjs.com/package/node-fetch ) is an excellent and easy to use option that was designed specifically for Node.js. node-fetch
( https://www.npmjs.com/package/node-fetch ) 是一个出色且易于使用的选项,专为 Node.js 设计。
Here is a simple example from its documentation:这是其文档中的一个简单示例:
fetch('https://github.com/')
.then(function(res) {
return res.text();
}).then(function(body) {
console.log(body);
});
You first call fetch()
with the URL you want.您首先使用所需的 URL 调用
fetch()
。 It'll return a Promise
with a Response
object.它将返回一个带有
Response
对象的Promise
。 From that, you can extract the text()
or anything else, and from there you can parse it.从那里,您可以提取
text()
或其他任何内容,然后您可以从那里解析它。
If you need to pass options, call a different method (like POST), etc., you'll just give it an options object as the second parameter to the first call (check out their documentation).如果您需要传递选项、调用不同的方法(如 POST)等,您只需给它一个选项对象作为第一次调用的第二个参数(查看他们的文档)。
There is a npm
module you can use that is based off jsdom
.您可以使用一个基于
jsdom
的npm
模块。
It will give you a window object.它会给你一个窗口对象。 Example:
示例:
var express = require('express');
var Window = require('window');
global.window = new Window();
global.document = window.document;
var $ = require('jquery');
var app = express();
function game() {
$("body").append(`<h1>Hello world!</h1>`);
}
game();
app.get('/', function (req, res) {
res.send(document.getElementsByTagName('html')[0].innerHTML)
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.