How to upload a video using multipart and Node.js?

1

I have an application that is being written using Node.Js, in it I use FFmpeg to create a video from a webcam. I need to upload it to the server using a POST request. So the question is:

How to upload a video using multipart and Node.js?

Ps1 .: The video is a local file whose path will be passed by parameter! Creating a form to choose the file is not the answer.

Ps2: I'm not using Express.

Ps3 .: The server runs on the internet, for this request receives the data in an address as: application.com.br/v1/videos. I have no access in which folder will be saved nor the port of communication. And it still needs to send parameters in the header and body of the request.

Edit 1: I was able to do the request using the request module. The problem is that I need to append an integer parameter in the request body. "testeId": 7451109 . How to proceed? Follow the code so far.

Edit 2: Updating the code.

Code:

request({
        method: "POST",
        url: "https://" + options.host + options.path,
        formData: {
            upload_type: "mp4",
            file: fs.createReadStream(options.nomeVideo)
        },
        headers: {
            'Content-Type': 'multipart/form-data',
            'access-token': chaveCriptografada
        }
    },

    function (error, response, body) {
        console.log(error);
        console.log(response);
        console.log(body);
    });
    
asked by anonymous 13.11.2017 / 16:42

1 answer

0

Hello, take a look at this lib.

link

Complete example:

link

This is an example without using express. To use it, all you have to do is just have a request that passes a file.

const http = require('http');

const hostname = '127.0.0.1';
const port = 3000;

var multer  = require('multer')

var storage = multer.diskStorage({
    destination: function (req, file, cb) {
        cb(null, '/tmp/my-uploads')
    },
    filename: function (req, file, cb) {
        cb(null, file.fieldname + '-' + Date.now())
    }
})
  
const server = http.createServer((req, res) => {
    var upload = multer({ storage: storage }).single('file');  

    upload(req, res, function (err) {
        if (err) {
            console.log(err);
            return
        }
        // Everything went fine
        res.statusCode = 200;
        res.setHeader('Content-Type', 'text/plain');
        res.end('Hello World\n');
    });    
});

server.listen(port, hostname, () => {
    console.log('Server running at http://${hostname}:${port}/');
});
    
13.11.2017 / 20:17