I want to add a new user to a json file by using the user input in a form I am able to upload the information to a txt file using the code below but I do not know how to do this using json, any help thank you
ejs file:
<!DOCTYPE html>
<html>
<head>
<title> users </title>
</head>
<body>
<h1>Users</h1>
<% include templates/header.ejs %>
<form action="http://127.0.0.1:3000/users" id="new" method="post" enctype='application/json'>
<div id="form">
<label for="lname">Last Name</label> <input type="text" id="lname" name="lname">
<label for="fname">First Name</label><input type="text"id="fname"name="fname">
<label for="id">ID</label><input type="text"id="id"name="id">
<button type="submit" id= "submit" value="Submit" >Submit</button>
</form>
</body>
</html>
user.js file:
var express = require('express');
var router = express.Router();
var fs = require("fs");
router.get('/', function(req, res, next) {
res.render('users');
});
//will send the user input
router.post('/', function(req, res) {
var first= req.body.fname;
var last= req.body.lname;
var id= req.body.id;
filePath = __dirname + '/people.txt';
fs.appendFile(filePath, JSON.stringify(first + " " + last + " "+ id ), function () {
res.end();
});
});
router.get('/listUsers', function (req, res) {
fs.readFile( __dirname + "/" + "users.json", 'utf8', function (err, data) {
console.log( data );
res.end( data );
});
})
module.exports = router;
div.