0

I have a node api where I need to take a input file which the user inputs. I am not sure what is the variable the input file will come in as. My curl command to test is : curl -d "@data.csv" -X POST http://localhost:8085/Upload

My node function "upload" which I am writing is :

'use strict';

const express = require('express');
var fs = require('fs');

// Constants
const PORT = 8085;
const HOST = '0.0.0.0';

// App
const app = express();
app.get('/', (req, res) => {
    res.send('Hello world\n');
});

app.listen(PORT, HOST);
console.log(`Running on http://${HOST}:${PORT}`);

app.post("/Upload", function(req, res) {
    var rand = Math.floor(Math.random() * 100000) + 1;
    console.log(rand);
    // console.log(req);
    // console.log(req.params)
    var data = fs.readFileSync('input.txt');
    console.log("Synchronous read: " + data.toString());
    });

I am not sure how to access the variable file "data.csv" which the user sends in nodejs code. This node code will again sends this "data.csv" to a different function. Please guide.

Dreams
  • 5,854
  • 9
  • 48
  • 71

2 Answers2

1

Your curl call looks like this:

Curl:

curl -d "@data.csv" -X POST http://localhost:8085/Upload

Node.js code:

var request = require('request');
var dataString = '@data.csv';

var options = {
    url: 'http://localhost:8085/Upload',
    method: 'POST',
    body: dataString
};

This means you are sending the file in the request body.

So your file is accessible in the (app.post("/Upload", function ... ) function as req.body variable.


Updated Answer:

Please modify your curl request as below:

curl -v -F csv=@data.csv http://localhost:8085/Upload

And check req.file variable.

Harshal Yeole
  • 4,812
  • 1
  • 21
  • 43
0

I partially solved the problem by importing a few required packages. I was able to read the file after adding them. But this still reads the file as a json. My app.js file now is :

'use strict';

const express = require('express');
const app = express();
var fs = require('fs');
var child_process = require('child_process');
var multipart = require("connect-multiparty");
// var execFile = require('child_process').execFile;
var cookieParser = require('cookie-parser');
var bodyParser = require('body-parser');
var methodOverride = require('method-override');
app.use(cookieParser());

// Required to add post parameters to the req.body
app.use(methodOverride('X-HTTP-Method-Override'));

app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));
app.use(cookieParser());
// Constants
const PORT = 8085;
const HOST = '0.0.0.0';

// App

app.get('/', (req, res) => {
    res.send('Hello world\n');
});

app.listen(PORT, HOST);
console.log(`Running on http://${HOST}:${PORT}`);

app.post("/Upload", function(req, res) {
    var rand = Math.floor(Math.random() * 100000) + 1;
    console.log(rand);
    // console.log(req);
    console.log(req.body);
    var data = fs.readFileSync('input.txt');
    console.log("Synchronous read: " + data.toString());
    console.log("reached here");
});
Dreams
  • 5,854
  • 9
  • 48
  • 71