I've been scratching my head over this for awhile. I have plans for a desktop application using Electron. Thus, I am developing an application using Node.js and Express.js . I have a simple app.js file that launches my site code:
var express = require("express");
var app = express();
app.use(express.static(__dirname + '/public'));
app.listen(3000, function () {
console.log('Example app listening on port 3000!');
});
Then I have a form on my page:
<form>
<input type="text" name="name">
<input type="text" name="url">
<button type="submit">Submit</button>
</form>
There is a jQuery event handler set on the submit button from a script.js file:
$(document).ready(function(){
var text;
$('form').submit(function( event ) {
text = $( this ).serializeArray();
console.log( text );
event.preventDefault();
});
});
How do I take the text I get from the form and write to a JSON file (that can be read again when the application is closed and reopened).
There is this question and answer: Writing files in Node.js
And I understand that, but how do I execute that code from my submit button?
My file structure is quite simple:
|- app.js
|- package.json
|- node_modules
|- public_
|- data.json
|- index.html
|- jquery.js
|- script.js
|- style.css
|- bootstrap.min.css
|- bootstrap.min.js