在Node.js和Html之间传递数据

在Node.js和Html之间传递数据,html,node.js,Html,Node.js,如何将“term”变量从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="informat

如何将“term”变量从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>

您要做的是设置一个服务器,该服务器将接收请求、执行搜索并返回结果。这是一个流行的框架

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.
    });
}
一旦您拥有了对象,您就可以处理它包含的信息并在页面中显示它

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.
    });
}