In Node.js variable assignment

In app.js

I have 3 words hardcoded:

var a = 'family';
var b ='friends';
var c ='teacher';

      

and saved them in an array named "List"

var List = [a, b, c];

      

Now I have passed this list in a track (Twitter API)

twit.stream('statuses/filter', { track: List }, function(stream) {
    stream.on('data', function (data) {
        // code
    });
}); 

      

Now I want to accept user keywords, so in index.html

I provide 3 textboxes (i.e. <input type="text">

)

When user enters the first keyword in textbox 1, it should be assigned var a

from app.js

, when the 2nd keyword is inserted, it should be assigned to var b

, etc.

<html>
<head>
</head>
<body>
    <form>
        Key 1<input id="Key_1" class="" name="" type="text" placeholder="First key" required /><br><br>
        Key 2<input id="Key_2" class="" name="" type="text" placeholder="Second key" required /><br><br>
        Key 3<input id="Key_3" class="" name="" type="text" placeholder="Third key" required /><br><br>

        <button type="submit" form="form1" value="Submit">Submit</button>
    </form>
</body>
</html>

      

How can i do this?

+3


source to share


1 answer


You can do something like this from this question: How to get GET variables (query strings) in Express.js on Node.js?

So when you submit the form, you can get the request parameters But first, you need to specify the name of each entry, for example a

, b

and c

:

<html>
<head>
</head>
<body>
<form method="post">

Key 1<input id="Key_1" class="" name="a" type="text" placeholder="First key" required /><br><br>
Key 2<input id="Key_2" class="" name="b" type="text" placeholder="Second key" required /><br><br>
Key 3<input id="Key_3" class="" name="c" type="text" placeholder="Third key" required /><br><br>

<button type="submit" form="form1" value="Submit">Submit</button>
</form>
</body>
</html>

      



Then you will need to make your HTML form submit data somewhere in your Node application and get the request values ​​using this:

app.post('/post', function(req, res){
  a = req.query.a;
  b = req.query.b;
  c = req.query.c;
});

      

+3


source







All Articles