Nodejs: How to pass user form data into defined class

0 votes

I am just trying build a blockchain after watching a youtube video.


const SHA256 = require('crypto-js/sha256');

class Block {
    constructor(data, previousHash = ''){
        this.previousHash = previousHash; = data;
        this.hash = this.calculateHash();

        return SHA256(this.previousHash + JSON.stringify(;

class Blockchain{
        this.chain = [this.createGenesisBlock()];

        return new Block("Genesis Block", "0");

        return this.chain[this.chain.length - 1];

        newBlock.previousHash = this.getLatestBlock().hash;
        newBlock.hash = newBlock.calculateHash();

        for(let i = 1; i < this.chain.length; i++){
            const currentBlock = this.chain[i];
            const previousBlock = this.chain[i - 1];

            if(currentBlock.hash !== currentBlock.calculateHash()){
                return false;

            if(currentBlock.previousHash !== previousBlock.hash){
                return false;
        return true;

let xyzcOin = new Blockchain();

xyzcOin.addBlock(new Block({amount: 8}));
xyzcOin.addBlock(new Block({amount: 80}));

// console.log("Is blockchain is valid? " + xyzcOin.isChainValid());

// xyzcOin.chain[1].data = {amout: 100};

// console.log("Is blockchain is valid? " + xyzcOin.isChainValid());

console.log(JSON.stringify(xyzcOin, null, 4));


<!DOCTYPE html>
<html lang="en">
    <meta charset="UTF-8">
    <title>xyzcOin Blockchain</title>
    <script type="text/javascript" src="app.js"></script>
    <div class="container">
        <div class="input">
            <form method="post">
                <label for="data">Please enter data:</label>
                <input type="text" name="data" id="data">
                <button type="submit" onclick="addData()">Add Data</button>
        <div class="result">
            <h4>Blocks Added</h4>
            <ul onload="appendData()">
                <li>Block 1:</li>
                <li>Block 2:</li>
                <li>Block 3:</li>
        <div class="validate">
            <button type="submit">Validate Chain</button>
            <p>Chain validated? true or false</p>


1) User entered data should be taken as data for a block(on submitting add data button) and it should append to listitem dynamically after data entered in above textbox.

2)To validate the blockchain for any tampering, I have created a button to validate the chain and result should be shown.

Sep 19, 2018 in Blockchain by slayer
• 29,040 points

1 answer to this question.

Your answer

Your name to display (optional):
Privacy: Your email address will only be used for sending these notifications.
0 votes

This should do your work:

// select the form
const formData = document.querySelector('.form-data');

// attach an event listener
formData.addEventListener('submit', (evt) => {

  const inputVal =[0].value;

  // enter url as first arg and data as second arg
  postData('', { data: inputVal })
  .then(data => {
     // JSON from `response.json()` call
    // do something with the data
  .catch(error => console.error(error))

// helper function for making posts with fetch api 
function postData(url, data) {
  return fetch(url, {
    body: JSON.stringify(data), 
    headers: {
      'content-type': 'application/json'
    method: 'POST', 
  .then(response => response.json()) // parses response to JSON
<form class="form-data">
    <label for="data">Please enter data:</label>
    <input type="text" name="data" id="data">
    <button type="submit">Add Data</button>
answered Sep 19, 2018 by digger
• 27,620 points

Related Questions In Blockchain

0 votes
1 answer

How to input realtime data from google sheets into urlfetch google scripts?

Based on your title I hope to ...READ MORE

answered Oct 12, 2018 in Blockchain by Perry
• 16,450 points
+1 vote
1 answer

How to store state data in Ethereum blockchain?

You won't have to overwrite the whole ...READ MORE

answered Apr 24, 2018 in Blockchain by Shashank
• 10,330 points
0 votes
1 answer
0 votes
1 answer

How to check the data integrity logic in proof of work mining?

The proof of work is actually works ...READ MORE

answered May 7, 2018 in Blockchain by Johnathon
• 9,050 points
0 votes
1 answer

Protocols used in a distributed/dlt system for the nodes to establish communication

yes all are over TCP/IP connections secured by TLS encryption in hashgraph architecture-hashgraph, ...READ MORE

answered Aug 6, 2018 in Blockchain by ariaholic
• 7,320 points
0 votes
1 answer

Truffle tests not running after truffle init

This was a bug. They've fixed it. ...READ MORE

answered Sep 11, 2018 in Blockchain by Christine
• 15,790 points
0 votes
1 answer
0 votes
1 answer

How i can use nodejs to watch transactions in bitcoin network?

you can use  const Socket = require(''); const mySocket ...READ MORE

answered Jul 9, 2018 in Blockchain by digger
• 27,620 points
0 votes
1 answer

© 2018 Brain4ce Education Solutions Pvt. Ltd. All rights Reserved.
"PMP®","PMI®", "PMI-ACP®" and "PMBOK®" are registered marks of the Project Management Institute, Inc. MongoDB®, Mongo and the leaf logo are the registered trademarks of MongoDB, Inc.