Recibo este error al usar el método POST con axios y formidable. Estoy tratando de cargar un archivo a una URL de API. Aquí está mi fragmento de código.
var server = http.createServer(function(req, response) { var filePath = '.'; filePath += req.url; var mimeType = { '.ico': 'image/x-icon', '.html': 'text/html', '.js': 'text/javascript', '.json': 'application/json', '.css': 'text/css', '.png': 'image/png', '.jpg': 'image/jpeg', '.wav': 'audio/wav', '.mp3': 'audio/mpeg', '.svg': 'image/svg+xml', '.pdf': 'application/pdf', '.doc': 'application/msword', '.eot': 'appliaction/vnd.ms-fontobject', '.ttf': 'aplication/font-sfnt' }; if (filePath == './') { filePath += "templates/index.html" } if (filePath == './success') { filePath = "templates/success.html" } var postUrl = "https://example.com/345678934/attachments"; const form = formidable({ multiples: true , keepExtensions : true}); const option = { method:'post', url: postUrl, headers : { 'content-type': 'application/json' } } axios.post(option, function(err, fields,files) { console.log("error from post: ",err) //response.writeHead(200, {'content-type': 'application/json'}); response.end(JSON.stringify({ fields, files }, null, 2)); console.log("Upload completed"); console.log(files); }).then(res => { console.log(`statusCode: ${res.status}`) console.log(res) }) .catch(error => { console.error("error from catch",error) }) fs.readFile(filePath, function(error, content) { if (error) { if (error.code == 'ENOENT') { response.writeHead(404); response.end('Page not Found'); } else { response.writeHead(500); response.end('Internal error: ' + error.code); } } else { var extname = path.extname(filePath); response.writeHead(200, { 'Content-Type': mimeType[extname] || 'text/plain' }); response.end(content, 'utf-8'); } }); }); server.on('listening', function(err) { if (err) throw err; console.log(Date() + ' Server is running...'); }); server.listen(7000);Aquí espero cargar todo tipo de extensión de archivo y también se pueden cargar varios archivos a esa URL de API.
Aquí está arrojando una excepción "error de error de captura: los datos después de la transformación deben ser una cadena, un ArrayBuffer, un Buffer o un Stream"
Tuve un error similar al usar FormData en la aplicación NestJS, no sé si esto te ayuda, pero para resolver mi problema hice los siguientes cambios:
import * as FormData from 'form-data'; import { Readable } from 'stream'; const form = new FormData(); form.append('file', Readable.from(file.buffer), { filename: file.originalname, })créditos a: https://stackoverflow.com/a/70660823/12216924