2

How can I pass the "term" variable from the html site to the node.js?

My HTML Page : 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>

My Node.JS : app.js I want to search with the 'term' variable for apps in the Playstore and give the information back to the html and print it there somewhere.

var gplay = require('google-play-scraper');

gplay.search({
    term: "Html 'Term' variable",
    num: 1,
    fullDetail: false,
    price: "free"
}).then(console.log, console.log);

1 Answer 1

2

What you want to do is set up a server that will take a request, perform the search, and send back the results. Express is a popular framework for doing this.

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

While your Node.js program is running, JavaScript code in your HTML page can use the fetch function to send it a request and get back the response.

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

Once you have your Response object, you can process the information it contains and display it in the page.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.