Passing variable from html form to nodejs

2

I am creating a webproxy with nodejs to be able to access "anonymously" to another site, so this is my server:

const http = require("http");
const url = require("url");
const request = require("request");
const fs = require('fs')
var server = http.createServer(onRequest);

function onRequest(req, res) {
  var queryData = url.parse(req.url, true).query;
  if (queryData.url) {
    request({ url: queryData.url })
      .on("error", function(e) {
        res.end(e);
      })
      .pipe(res);
  } else {
    fs.readFile("index.html", (err, html) => {
      if (err) {
        console.log(err)
      }else {
        res.write(htm);
        res.end();
      }

    });
  }
}

server.listen(80);

Fortunately, my eyesight loads:

<!DOCTYPE html>
<html lang="en" dir="ltr">
  <head>
    <meta charset="utf-8">
    <title>Hola</title>
  </head>
  <body>
    <div class="container mt-5">
      <div class="row mt-5">
        <div class="mx-auto mt-5">
          <div class="form-group">
            <form class="" method="post">
              <input type="text" class="form-control" id="url" placeholder="www.example.com">
            </form>
            </div>
        </div>
      </div>
    </div>
  </body>
</html>

Even the proxy works for me, since I would only have to add the url of the page I want to visit to the variable called url in the header as follows: link and go, get it right. What I want to do is a kind of get request for the url variable to access the input value of the form, but I do not have the slightest idea. Does anyone have any ideas?

    
asked by Diesan Romero 23.08.2018 в 02:58
source

1 answer

2

Try this:

<form action="" method="get">
  <input type="text" class="form-control" name="url" placeholder="www.example.com">
  <button type="submit">Enviar</button>
</form>

Notice the property action and method of <form> . With action we tell you what url to make the request and with method how to do it, in this case get .

Now in the input we put the names of the values that we want to send with the property name (in this case url ).

    
answered by 23.08.2018 / 05:06
source