export function node in angular 4?

0

Dear, I need to process an xlsx file and parsed it to json and my idea is to do it in the view with angle 4, I found a library in node which does the process (node-xlsx-json) but I do not know if I could call it from angular or able to do something since the library reads the file locally and processes it.

var xlsx_json = require('../')

xlsx_json({
  input: __dirname + '/interview.xlsx',
  output: __dirname + '/test.json'
}, function(err, result) {
  if(err) {
    console.error(err);
  }else {
    console.log(result);
  }

});

Or some way to do it.

Thank you.

    
asked by kast 08.09.2017 в 01:54
source

1 answer

0

You can make an endpoint on the server, and connect to it from the angle on the client. So you pass the xlsl to the server, let him do all the work and then give him the answer (json) to angular, for the client it is a transparent process.

You would have something like this on the server:

const fs = require('fs');
const xlsx_json = require('../');

const input = __dirname + '/interview.xlsx';
const output = __dirname + '/test.json';

function getJson(cb) {
    xlsx_json({
        input,
        output,
    }, cb);
}

app.use("/parse-xlsx", (req, res) => {
    const file = req.file;

    fs.writeFile(input, file, (err) => {
        // handle err
        getJson((err, result) => {
            // handle err
            res.send(result);
        });
    });

});
    
answered by 13.09.2017 в 16:44