Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

102
Visualizações
Express App Only Display JSON on Windows, Not Debian or Heroku

I have created an express app which successfully displays JSON on Windows - I have had no problem on Firefox or Chrome. However, when I try to run the same code on my linux setup (Debian) or deploy it on Heroku, receives an empty array: []. How can I get it to display on more than just windows? Here is the js file:

var express = require('express');
var router = express.Router();
var fs = require('fs');

//read file of chore todos
function readCSV(callback) {
  fs.readFile('./data/todoList.csv', function(err, data) {
    if(err) throw err;
    var dataStr = data.toString();
    var array = dataStr.split("\n");
    fs.writeFileSync('./data/todo.json', JSON.stringify(convertToJSON(array), null, 4));
    var temp = convertToJSON(array);
    return callback(temp);
  });
}

//convert array to JSON
function convertToJSON(array) {
  var rows = array.toString();
  rows = rows.split("\r");
  for (var i=1; i<rows.length;i++) {
    rows[i] = rows[i].replace(rows[i][0], "");
  }
  var asJSON = [];
  // get column headers
  var headers = rows[0].split(",");
  
  for (var i=1; i<rows.length-1; i++) {
    var obj = {}
    var currentRow = rows[i].split(",");

    for (var j=0; j<headers.length; j++) {
      obj[headers[j]] = currentRow[j];
    }
    asJSON.push(obj);
  }
  return asJSON;
}

var returnJSON;
readCSV(function(temp) {
  returnJSON=temp;
})

router.get('/', function (req, res, next) {
  res.json(returnJSON);
});

module.exports = router;
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

The problem is caused by the below line in convertToJSON() function

rows = rows.split("\r");

Consider the following csv file created in windows.

enter image description here

And its binary representation: enter image description here

You will notice that the newline character is denoted by "\r \n" or CR LF characters

If the same csv is created in Linux, its binary version will look like below: enter image description here Here the newline character is denoted by "\n" or LF character.

Different operating systems handle the newline character differently. Refer https://en.wikipedia.org/wiki/Newline for more details.

To get it working for Linux, you can change to below

rows = rows.split("\n");

However, this will not work for windows.

How can I get it to display on more than just windows?

Use csvtojson to handle the edge cases like these and most of the heavy lifting. However if you want to handle it on your own you can check how the package handles it here

https://github.com/Keyang/node-csvtojson/blob/master/src/getEol.ts

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda