I have a file with 10 variables stored in it, locally on my system. I want this to be available to someone else when they connect to my system. This will be used within my LAN only and not over the internet so there is no need to worry about security. I want to create a simple client-server application that uses HTTP request/response to communicate.
I can work with HTML and Javascript but I have limited knowledge of Node JS. I have created a HTML page with 10 blank labels and a "show" button. When I click on the button, I want the file to be read and each of the 10 variables to be displayed in the labels. Here is a sample with two variables and a button.
**index.html**
<html>
<body>
<form method="post" action="/">
<fieldset>
<legend><h2>Parameters:</h2></legend>
System ID : <textarea name="my_id" id="my_id" size="2"></textarea><br >
System IP : <textarea name="my_ip" id="my_ip" size="15"></textarea><br>
<input id="show" type="submit" name="show" value="SHOW"><br>
</fieldset>
</form>
</body>
</html>
How do I go about this? Is there any easier way besides Node JS ? If this works I would also like to over-write the variables in the same file with the user's inputs (using a different "submit" button).
I would recommend just using the http module along with the fs module for this but since you are still learning nodejs, I would recommend expressjs and flat-file-database
So when you are creating routes for your small webapp, it would probably look like this :
// Serves html file
app.get('/', function(req, res) {
res.sendFile('/index.html');
});
// Stores data from the form
app.post('/form', function(req, res) {
var data = req.body;
db.put('data', data); // Saves data to the flat file db
res.status(201);
});
// Gets data from the db
app.get('/form', function(req, res) {
var data = db.get('data');
res.status(200).json(data);
});
You would need to 'post' the data at /form in your client side code. To get the data, simply issue a get request at /form
Also keep in mind that for express to parse form data, you need the body-parser middleware. Oh and note that I have no idea whether flat-file db is asyncronous or not so you will probably need to check that yourself.