0

I want to get a .json file from server to .js file in ClientSide

var fs = require('fs');
var bodyParser = require('body-parser');
var urlencodedParser = bodyParser.urlencoded({extended: false, limit: '50mb'});
module.exports = function(app){
  app.get('/tables_particular/', function(req, res){

    first = JSON.parse(fs.readFileSync('./database/table.json'))

    res.render('tables_particular', {todos: data.member, 
                                     tbledit: first,
                            });
});

app.post('/tables_particular/', urlencodedParser, function(req, res){

    var exportlist = req.body.detail

    fs.writeFileSync('./database/table.json', exportlist); 
        res.json(exportlist);

});

and here is the app.js: (server code)

var express = require('express');

var mShController = require('./controllers/mShController');

var app = express();

//
app.set('view engine', 'ejs');

//static files
app.use(express.static('./public'));

mShController(app);


//listen to port
app.listen(3000);
console.log('You are listening to port 3000');

what is the code I need to use in javascript file to get the json file.

I need it in js file:

var data = response.tbledit
Syed Ayesha Bebe
  • 1,797
  • 1
  • 15
  • 26

1 Answers1

0
var content;  
var fs = require('fs');
fs.readFile('path/myfile.json', function read(err, data) {
    if (err) {
        throw err;
    }
    content = data;

    console.log(content); // HERE DO ANYTHING WITH THE JSON DATA
});

OR See this link read-json-file-content-with-require-vs-fs-readfile

var myObj = require('path/myfile');
var value = myObj.someValue;
});
Sudhanshu Gaur
  • 7,486
  • 9
  • 47
  • 94