There is a Node JS server - expressJS, which receives a picture from the POST post request. Next, you need to send this picture to another server (microservice).

I successfully receive the picture, but I cannot send it further.

Then a small code for the test, in which the first API receives a picture from the browser and immediately sends it to the second API.


package.json dependencies

"dependencies": { "lodash": "latest", "nunjucks": "latest", "redis": "latest", "connect-redis": "latest", "express": "latest", "express-session": "latest", "cookie-parser": "latest", "body-parser": "latest", "query-string": "latest", "multer": "latest", "request": "latest" } 


index.js

 const http = require('http'); const request = require('request'); const express = require('express'); const multer = require('multer'); const bodyParser = require('body-parser'); const upload = multer(); var app = express(), server = http.createServer(app); /** * Set body parser */ app.use(bodyParser.json()); app.post('/test1', upload.single('file'), (req, req) { console.log('test1 file ->',req.file.buffer); // Here Buffer request.post( { url: 'http://localhost:3000/test2', formData: { avatar: req.file.buffer } }, (err, httpResponse, body) => { console.log(); console.log('test1 err', err); // No errors console.log('test1 body', body); // {success: true} console.log(); } ); res.json({success: true}); }); app.post('/test2', upload.single('avatar'), (req, req) { console.log('test2 file ->', req.file); // undefined console.log('test2 avatar ->', req.avatar); // undefined console.log('test2 files ->', req.files); // undefined console.log('test2 body ->', req.body); // {} res.json({success: true}); }); /** * Run server */ server.listen(3000); 

To send a POST request to / test2 I use npm package request In the second request I see that nothing came. If anyone encountered, specify how to do it correctly. I have so far the options are over.

PS option to save the file to disk, and then consider it fs.createReadStream is not beautiful.

  • one
  • but it is more correct to immediately upload the file to 3002, for example, by proxying the "upload" requests with the front-end. or, at worst, use webdav instead of a post request for communication between 3000 and 3002 - nörbörnën
  • Yes thank you! I saw this post. From it and got to the documentation. And he did not. Only here did not work. Changed the code a bit and took off. Proxy is not an option, as before downloading, etc., access checks and so on go. And transferring logic to another place is not an option. - Aries Ua

1 answer 1

I initially did the documentation. But @norborner advised to read carefully. As a result, I changed the sending code to a different code.

 let r = request.post('http://test.loc:3000/test2', (err, httpResponse, body) => { console.log(); console.log('err', err); console.log('body', body); console.log(); res.json({success: true}); }); let form = r.form(); form.append('avatar', req.file.buffer, { filename: req.file.originalname, encoding: req.file.encoding, mimetype: req.file.mimetype, size: req.file.size }); 

Then everything went off successfully. Thanks for the help!