Tejas_hooray
Tejas_hooray

Reputation: 636

Parsing CSV From Variable Instead Of File Node JS

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'))



Upvotes: 1

Views: 107

Answers (1)

Duncan_m
Duncan_m

Reputation: 2546

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

Upvotes: 1

Related Questions