title: How can I return a value from a Javascript file to a Node.js so I can write to a file?
body: <p>I have a Javascript file which reads the inputs of a form and puts it into a JSON string. I want to create a file and write the JSON string to the file. I know how to write to files using Node but I am wanting to know how I can pass the string to my server.js file.</p>
  
  <p>Can anyone help?</p>
  
  <p>Javascript file (script.js)</p>
  
  <pre><code>(function() {
  function toJSONString( form ) {
      var obj = {};
      var elements = form.querySelectorAll( "input, select, textarea" );
      var store = elements[0];
      var configValue = store.value;
      for( var i = 1; i &lt; elements.length; ++i ) {
          var element = elements[i];
          var name = element.name;
          var value = element.value;
          if( name ) {
              obj[ name ] = value;
          }
      }
      console.log(configValue);
      return JSON.stringify({ [configValue] : [ obj ]  } );
  }
  
  document.addEventListener( "DOMContentLoaded", function() {
      var form = document.getElementById( "test" );
      var output = document.getElementById( "output" );
      var fs = require('fs');
      form.addEventListener( "submit", function( e ) {
          e.preventDefault();
          var json = toJSONString( this );
          output.innerHTML = json;
      }, false);
  
  });
  
  })();
  </code></pre>
  
  <p>Node.js file (server.js):</p>
  
  <pre><code>const socketIO = require('socket.io');
  const path = require('path');
  const INDEX = path.join(__dirname, '/public/index.html');
  
  const PORT = process.env.PORT || 3000;
  
  var express = require('express');
  var fs = require('fs');
  var script = require('./public/script.js');
  
  const app = express()
  .get('/', function (req, res) {res.sendFile(INDEX)})
  .use(express.static(__dirname + '/public'))
  .listen(PORT, () =&gt; console.log(`Listening on ${ PORT }`));
  
  const io = socketIO(app);
  </code></pre>