0

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;
2
  • You want to write JSON document in a file? Commented Jun 22, 2016 at 21:10
  • Side note, you didn't close your div. Commented Jun 22, 2016 at 21:16

1 Answer 1

1

I would recommend against using a file to do this. However if you insist you can always save the data in a standard JSON format. In order to do this you can do something like:

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;

   var person = {
     first: first,
     last: last,
     id: id
   }; // if you are using es6 you do var person = {first, last, id}

  var filePath = __dirname + '/people.json';
  fs.readFile(filePath, function (err, file) {
    if (err) {
      res.status = 500;
      res.send(err).end();
      return;
    }

    var fileString = file.toString();
    var people = fileString ? JSON.parse(fileString) : [];
    people.push(person);
    fs.writeFile(filePath, JSON.stringify(people), function(err) {
       if (err) {
        res.status = 500;
        res.send(err).end();
        return;
      }

      res.end();
    });
  });
});



router.get('/listUsers', function (req, res) {
  fs.readFile( __dirname + "/" + "users.json", 'utf8', function (err, data) {
    if (err) {
      res.status = 500;
      res.send(err).end();
      return;
    }
    res.json(JSON.parse(data));
  });
})



module.exports = router;
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.