-1

I created the following function to load a stream file. This stream is sent to the address specified with a POST. At the destination, I have another NodeJS service that with ExpressJS "capture" this POST but I have no idea how I can save the file to the destination.

NodeJS on my PC:

function sendFileToRaspberry(filePath) {
    var options = {
        method: 'POST',
        url: 'http://x.x.x.x:8080/api/print',
        qs: {file: fs.createReadStream(filePath)},
        headers:
            {
                'cache-control': 'no-cache'
            }
    };

    request(options, function (error, response, body) {
        if (error) throw new Error(error);

        console.log(body);
    });
}

NodeJS on my Raspberry:

app.post('/api/print', function (req, res) {

    ORIGINAL_IP = req.ip.replace("::ffff:", '');
    console.log("Received a print request from: " + ORIGINAL_IP);

    // Test history data for /api/printer API
    var file = req.query.file;
    console.log("Test data: " + file);
    util.inspect(file);

});

But in the console log I get:

Received a print request from: y.y.y.y

Test data: [object Object]

How can I save this file on Rasp?

Community
  • 1
  • 1
shogitai
  • 1,823
  • 1
  • 23
  • 50

1 Answers1

-1

You can use a expressjs middleware helper like express-fileupload, multer.

I think, you have use Multipart Form Uploads to upload a file via http POST.

Client side (Your PC)

function sendFileToRaspberry(filePath) {
    var options = {
        method: 'POST',
        url: 'http://x.x.x.x:8080/api/print',
        formData: {
            file: fs.createReadStream(filePath)
        },
        headers: {
            'cache-control': 'no-cache'
        }
    };

    request(options, function (error, response, body) {
        if (error) throw new Error(error);

        console.log(body);
    });
}
hoangdv
  • 15,138
  • 4
  • 27
  • 48