Node.js Express Fake Data Generator in CSV File App Using Faker.js and objects-to-csv Library Full Tutorial

Node.js Express Fake Data Generator in CSV File App Using Faker.js and objects-to-csv Library Full Tutorial

 

Welcome folks today in this tutorial we will be building a fake data generator app using node.js and express using a library called as faker.js and objects-tocsv All the source code of the application is given below. A step by step youtube video is also shown below.

 

 

 

Live Demo

 

 

Get Started

 

In order to get started you need to install these dependencies

  1. Express
  2. body-parser
  3. faker
  4. objects-tocsv
  5. fs
  6. ejs

 

npm i express faker objects-to-csv body-parser ejs

 

Source Code

 

First of all you need to create your index.js file which is the starting point of the application and copy paste the full below code to it

 

const express = require('express')

const objectstocsv = require('objects-to-csv')

const fs = require("fs")

const app = express()

const faker = require('faker')

const bodyParser = require('body-parser')

const PORT = process.env.PORT || 5000

app.set("view engine","ejs")

app.use(bodyParser.urlencoded({extended:false}))
app.use(bodyParser.json())

app.get('/',(req,res) => {
    res.render("index")
})

app.post('/',async(req,res) => {

    var data = []

    var number = req.body.number
   

    for(var i=0;i<number;i++){
        data.push(
            {
                name:faker.name.findName(),
                email:faker.internet.email(),
                imageUrl:faker.image.imageUrl(),
                password:faker.internet.password()
            }
        )
    }

    // convert to csv file

    const csv = new objectstocsv(data);
 
  // Save to file:
  await csv.toDisk('./test.csv');
 


  res.download("./test.csv",() => {

    fs.unlinkSync("./test.csv")

  })



})


app.listen(PORT,() => {
    console.log("app is listening on port 5000")
})

 

See also  Build a GraphQL Server CRUD API in Node.js and Express Using Express-GraphQL Library Full Project For Beginners

 

Now we need to build the template file called as index.ejs which will be located under views folder so make a new folder views inside the root directory of your project.

 

views/index.ejs

 

<!DOCTYPE html>
<html>
  <head>
    <title>Fake Data Generator</title>
    <link
      rel="stylesheet"
      href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css"
    />
  </head>
  <body>
      <div class="container">
          <br><br><br><br><br><br><br><br><br>
          <h1 class="text-center">Fake Data Generator</h1>
          <br><br>
          <form action="/" method="post">
            <div class="form-group">
                <input type="number" class="form-control" name="number" id="" placeholder="No of Rows" required>
            </div>
            <br>
            <div class="form-group">
                <button class="btn btn-danger btn-block">
                    Generate Data
                </button>
            </div>
        </form>
      </div>
</body>
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
</html>

 

 

Screenshots

 

 

 

 

 

 

 

DOWNLOAD SOURCE CODE

 

 

Leave a Reply