-1

I want to use form input from HTML to JS. For that i am using following snippet but getting "SyntaxError: Unexpected token if" can anyone help in modifying it.

var http = require('http');
var url = require('url');
var fs = require('fs');

http.createServer(function(req, res) {
    fs.readFile(html_form_path, function(err, data) {
        res.writeHead(200, {'Content-Type': 'text/html'});
        res.write(data);
        res.end();
    });
    var q = url.parse(req.url, true);
    var qdata = q.query;
    res.write(qdata.Input());
}).listen(8080);

HTML FORM:

<html>

<body>
    <form type='get'>
        <input type="text" name='Input'>
        <input type='submit'>
    </form>
</body>

</html>
aman
  • 33
  • 2
  • Try using body-parser with express. Check this post: https://stackoverflow.com/questions/15568851/node-js-how-to-send-data-from-html-to-express – mr-woot Dec 02 '18 at 15:32
  • you dont need fs module, you nees body-parser and express or some other framework will make it easier to receive request – Mladen Skrbic Dec 02 '18 at 15:33
  • The code you've shared doesn't mention `if` at all. The error message is coming from somewhere else. – Quentin Dec 02 '18 at 18:46
  • @TusharMudgal — It is a GET form, there is no body to parse with body-parser. – Quentin Dec 02 '18 at 18:48
  • @MladenSkrbic — How are they supposed to read the file without the `fs` module? They don't need `body-parser` as there is no request body to parse. While I'd generally recommend Express, there's no major benefit to adding it in here. – Quentin Dec 02 '18 at 18:49
  • The code you've provided throws the error `ReferenceError: html_form_path is not defined` not the one you say it throws. You need to provide a [mcve] – Quentin Dec 02 '18 at 18:52

1 Answers1

-1

You should send data from html to server, you will need bodyparser and it is easiest to do if you also have express on server then you make submit button and i form tag add action and method atributes and on server you expect

app.post("/action",function(req,res){
    req.body.input
})

html:

<form action='/action' method='POST'  >
    <input name=input''>
    <input type='submit'>
</form>

also this will reload your page, if you want not to reload you need use ajax to send data you can use axios in javascript. see express docs on how to set it up and you need see bodyparser docs and then use this code

Aplet123
  • 33,825
  • 1
  • 29
  • 55
Mladen Skrbic
  • 154
  • 2
  • 12
  • "you will need bodyparser" — That's only true if the data is being POSTed … which it isn't in the code in the question. – Quentin Dec 02 '18 at 18:47
  • this is not how you make get request and if it is not post request why he uses input... and also why would he use server for this anyway – Mladen Skrbic Dec 02 '18 at 18:52
  • "this is not how you make get request" — Yes it is. (Well…ish. The `type` attribute value is nonsense but will be ignored because it only applies to forms with `method="POST"`) – Quentin Dec 02 '18 at 18:53
  • "if it is not post request why he uses input" — So that the query string contains data provided by the user of the browser. – Quentin Dec 02 '18 at 18:53
  • "and also why would he use server for this anyway" — I have no idea what the OP's end goal is. The code appears to be an attempt to learn how to read query strings. – Quentin Dec 02 '18 at 18:54
  • There *are* lots of problems with the code, but the basic concept of using a form to make a GET request is not one of them, and none of the problems in the code appear to be related to the problem described in the question. – Quentin Dec 02 '18 at 18:56
  • this needs to be post request and then u send response back – Mladen Skrbic Dec 02 '18 at 18:57
  • It does not need to be a POST request. Forms are used for GET requests all the time. Type something into the Search box at the top of *this* page and press enter. It's a form. It makes a GET request. Form triggered GET requests are one of the basic building blocks of the WWW. – Quentin Dec 02 '18 at 18:58
  • var express = require('express'); var app = express(); app.get('/', function(req, res){ res.send('id: ' + req.query.id); }); app.listen(3000); – Mladen Skrbic Dec 02 '18 at 19:03
  • this is how he should make it – Mladen Skrbic Dec 02 '18 at 19:03