我如何通过"术语"从html站点到node.js的变量?
我的HTML页面:search.html
<html>
<body>
<div id="content">
<p>Name:</p>
<input id="begriff" type="name" name="" value="">
<button style="margin-top: 50px;" onclick="information()" >send</button>
</div>
<script type="text/javascript">
var term;
function information() {
term = document.getElementById("name").value;
}
</script>
</body>
</html>
我的Node.JS:app.js 我想搜索“&#39; term&#39; Playstore中的应用程序的变量,并将信息提供回html并将其打印在某处。
var gplay = require('google-play-scraper');
gplay.search({
term: "Html 'Term' variable",
num: 1,
fullDetail: false,
price: "free"
}).then(console.log, console.log);
答案 0 :(得分:0)
您要做的是设置一个服务器,该服务器将接收请求,执行搜索并发回结果。 Express是一个流行的框架。
var gplay = require('google-play-scraper');
var express = require('express');
var app = express();
app.get('/search', function (req, res) {
// This will run every time you send a request to localhost:3000/search
var term = req.params.term;
gplay.search({
term: term,
num: 1,
fullDetail: false,
price: "free"
}).then(function(result) {
// Process the result however you want.
// To send a response back, do this:
res.send("Whatever you want to send back");
});
})
// Tell Express what port to listen on
app.listen(3000);
当您的Node.js程序正在运行时,HTML页面中的JavaScript代码可以使用fetch
函数向其发送请求并获取响应。
function information() {
var term = document.getElementById("begriff").value; // The id of your input was "begriff", not "name"
var url = 'http://localhost:3000/search?term=' + term;
// Now send a request to your Node program
fetch(url).then(function(res) {
// Res will be a Response object.
// Use res.text() or res.json() to get the information that the Node program sent.
});
}
获得Response对象后,您可以处理其中包含的信息并将其显示在页面中。