2

I have a very simple file uploader script in which I want to append some logs to a nodejs server of mine. It successfully gets the data of the file but it fails to change line at server-side.

This is my script so far:

var http = require('http');
var fs = require('fs');

http.createServer(function (req, res) {
        if(req.method== 'POST'){
                console.log('bhke sto POST');
                if (req.url == '/logs/upload/Accelerometer.csv') {
                        console.log("Calledeixa to swsto path");

                        var body="";
                        req.on('data', data=>{

                        body+=data;
                        console.log("partial data ="+data);

                        fs.appendFile('Accelerometer.csv', body, function (err) {
                                console.log('Saved!');
                        });


                        });

                        console.log(body);

                        req.on('end', function(){
                        res.writeHead(200, {'Content-Type' : 'text/html'})
                        res.end('Accelerometer.csv was updated successfully\n');
                        });

                } else {
                        res.writeHead(200, {'Content-Type': 'text/html'});
                        res.write('<form action="fileupload" method="post" enctype="multipart/form-data">');
                        res.write('<input type="file" name="filetoupload"><br>');
                        res.write('<input type="submit">');
                        res.write('</form>');
                        return res.end();
                }
        }
}).listen(8080);

I debug it using curl and a random file. The command I use is curl -X POST -d @filename 127.0.0.1:8080/logs/upload/Accelerometer.csv

And the contente of the file are:

0,0,0

1,2,3

2,4,7

But at server-side I get this:

enter image description here

How can I make the server-side file have the same format as the original?

George Sp
  • 553
  • 5
  • 20

1 Answers1

1

It's simply because curl removes linebreaks from data posted with the -d option. Use --data-binary options instead.

Here's more SO answer about sending newline with curl.

1565986223
  • 6,420
  • 2
  • 20
  • 33