1

I need to get data from simple html form (code below) and send it to API (http://netology.tomilomark.ru/doc/#api-ND) that makes hash out of it. Here is html form code:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Form</title>
</head>
<body>
 <form action="/sendForm">
    Name:<br>
    <input type="text" name="firstName" value="">
    <br>
    Surname:<br>
    <input type="text" name="lastName" value="">
    <br><br>
    <input type="submit" value="Send">
 </form>
</body>

And that's what I´ve got on the server side:

"use strict";

const http        = require("http");
const fs          = require("fs");
const PORT        = 3000;

let resObject = {};
let hash;
// Code that sends name + surname to the API and creates hash 
// ------------- begin -------------
let options = {
  hostname: "netology.tomilomark.ru",
  path: "/api/v1/hash",
  method: "POST",
  headers: {
    "firstName": "Evgeny",
    "Content-Type": "application/json"
  }
};

let req = http.request(options, (res) => {
  let resString = "";
  res.on("data", (data) => {
    resString += data;
  });
  res.on("end", () => {
    console.log(resString);
    hash = resString["hash"];
  })
});

let reqMessage = JSON.stringify({"lastName": "Kobzev"});

req.write(reqMessage);
req.end();

resObject.firstName = "Evgeny";
resObject.lastName = "Kobzev";
console.log(JSON.stringify(resObject));
// -------------- end --------------

// Create web server that loads the html file
// ------------- begin -------------
const server = http.createServer((req, res) => {
  fs.readFile("./logs/form.html", (err, file) => {
    res.writeHead(200, {'Content-Type': 'text/html','Content-Length':file.length});
    res.write(file);
  });
});
server.on("error", (err) => console.error(err));
server.on("listening", () => console.log(`Start HTTP on port ${PORT}`));
server.listen(PORT);
// -------------- end --------------

How can I get data from that simple form and later send it to API? The problem is that I need to use a low level abstraction solution: http and maybe querystring.

Any help will be appreciated!

2
  • You want to get the names in response or hashed values???Or simply you want the data that you entered in form???? Commented Aug 9, 2017 at 13:37
  • @SyedAyeshaBebe this API gives me hash + value like this: {"hash":"04c1bc3782871a2801abf121e3ea45f8"}. I can later pack my name, surname, hash into a object. There is no problems with that Commented Aug 9, 2017 at 15:30

1 Answer 1

1

After working for few minutes I get the code.Here in my code I am printing hash values in terminal.And here is my code

var qs = require('querystring');
var request=require('request');
var util=require('util');
const http= require("http");
const fs= require("fs");
var hash=require('object-hash');
const server = http.createServer((req, res) => {
    if (req.url === '/sendForm' && req.method === 'POST') {
        var body = "";
  req.on('data', function (chunk) {
    body += chunk;
    });
  req.on('end', function () {
  var post= qs.parse(body);
    var Fs=hash({NAME:post.firstName});
    var Sn=hash({SURNAME:post.surName});
    console.log("FirstName:"+post.firstName);
    console.log("SurName:"+post.surName)
    console.log("Hashed Value Of FirstName:"+Fs);
    console.log("Hashed Value Of SurName:"+Sn);
  res.end("successfully submitted");
     });

    }
 fs.readFile("./sample.html", (err, file) => {
    res.writeHead(200, {'content-type': 'text/html','Content-Length':file.length});
  res.end(file);

  });
 }).listen(3000,function(){
     console.log("Server Listening on 3000");
 });

Hope this helps for you...

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.