1

I am building a web app that involves sending an http request to a site which returns CSV data. I'm then trying to parse that in Node JS so I can render it as a table with Handlebars (I'm using Express, not sure that really matters though). To parse that CSV data, I've only found ways to parse it from a file. Is there anyway to parse it from a variable (the data that gets returned from my request)?

Thanks

EDIT: Code:

const express = require('express');
const expbs = require('express-handlebars');
const helpers = require('./helpers');
const https = require('https');
const csv = require('csv-parse');



const app = express();

app.use(express.static('static'))

const hbs = expbs.create({
    defaultLayout: 'main',
    helpers: {
        last: helpers.last,
        findchange: helpers.findchange
    }

})


app.engine('handlebars', hbs.engine);
app.set('view engine', 'handlebars')

app.get('/', (req, res) => {
    let body = [];
    https.get('https://raw.githubusercontent.com/CSSEGISandData/COVID-19/master/csse_covid_19_data/csse_covid_19_time_series/time_series_covid19_confirmed_global.csv', dataRes => {
        dataRes.on('data', (data) => body += data);
        dataRes.on('end', () => {
            // Trying to parse here
        });
    })
    

})



app.listen(5000, () => console.log('Listening on port 5000'))



1
  • 1
    @ggorlen edited original post Commented Aug 5, 2020 at 4:59

1 Answer 1

1

You can split any string into an array with the Javascript "split" operator, you'd use "," as your delimiter

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/split

Sign up to request clarification or add additional context in comments.

1 Comment

While thats a good idea, it didn't work because I need each row as an array.. when I do that I get one large array with all the rows

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.