0

I have a project in Node JS in which I have a form to add new users.

How can I view this information in JSON format?

These are the data that I see:

name   age   country   city
------------------------------
user1   22    Spain    Madrid      button{View JSON}

When I press the 'View JSON' button, the following must be displayed below the table:

[
   "id": 1,
   "name": "user1",
   "age": 22,
   "country": "Spain" {
      "city":"Madrid"
   }
]

My problem: how can I create a function that performs this conversion? How do I call the function from index.ejs?

1 Answer 1

1

I cleared and merged the codes. And I created a new endpoint as /export to export the data as CSV file. I couldn't test it so let me know if it doesn't work.

const express = require('express');
const bodyParser = require('body-parser');
const app = express();
const MongoClient = require('mongodb').MongoClient;

app.use(bodyParser.urlencoded({ extended: true }));
app.use(bodyParser.json());
app.use(express.static('public'));
app.set('views', './src/views');

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

    const db = await mongoDB();

    const person = await db.collection('person').find().toArray();

    res.render('index.ejs', { person: person })
})

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

    await convertCSV();

    res.status(200).send( { success: 1 });
})

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

    res.redirect('/');
})

app.listen(process.env.PORT, function () {
    console.log(`server: http://${process.env.HOST}:${process.env.PORT}`);
})


const mongoDB = () => {
    return new Promise((resolve, reject) => {
        const url = 'mongodb://127.0.0.1:27017';
        MongoClient.connect(url, { useUnifiedTopology: true })
            .then(client => {
                const db = client.db('users')
                resolve(db);


            })
            .catch(error => reject(error))
    });
}


const convertCSV = () => {
    return new Promise((resolve, reject) => {
        const converter = require("json-2-csv");
        const fetch = require("node-fetch");
        const fs = require("fs");
        const flatten = require('flat');
    
        const maxRecords = 10;
    
        const getJson = async () => {
            const response = await fetch(`http://${process.env.HOST}:${process.env.PORT}/users.json`);
            const responseJson = await response.json();
            return responseJson;
        };
    
        const convertToCSV = async () => {
            const json = await getJson();
            let keys = Object.keys(flatten(json[0]));
            let options = {
                keys: keys
            };
            converter.json2csv(json, json2csvCallback, options);
        };
    
        let json2csvCallback = function (err, csv) {
            if (err) throw err;
            const headers = csv.split('\n').slice(0, 1);
            const records = csv.split('\n').slice(0,);
            for (let i = 1; i < records.length; i = i + maxRecords) {
                let dataOut = headers.concat(records.slice(i, i + 3)).join('\n');
                let id = Math.floor(i / maxRecords) + 1;
                fs.writeFileSync('data' + id + '.csv', dataOut)
            }
        };
    
        await convertToCSV();
        resolve();
    })
}

However, it is not a good practice at all to using controller, index and route in the same file. A better approach would be to create routes, controllers folders and put the codes in a more orderly form.

Something like this (You can find better ones of course mine is just advice):

 - index.js
 - router.js (A router to manage your endpoints)
 - controllers (Controller when you call the endpoint)
  -> export.controller.js
  -> person.controller.js
 - routes (Endpoints)
  -> export.route.js
  -> person.route.js
 - helpers
  -> databaseHandler.js (Database connection handler)
Sign up to request clarification or add additional context in comments.

4 Comments

I have been modifying the folder structure to leave it as you say separately. On the other hand, the export CSV button does not work for me, I have called the convertCSV () function in the html but I can't get it to enter
And html does not read the variables, I have added a function in the index: function test() { console.log('view JSON'); } and in the html: <button id="viewJson" onclick="test()">View JSON</button>, but it does not enter
I'm sorry. I do not know much about HTML-nodejs communication. But, It's interesting that the javascript you added doesn't work. Are you sure you are correctly importing javascript file?
I have updated the question to go by parts because I cannot execute the complete code

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.