2

I have the following client service:

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

var file = "c:\\log.txt";
var url = "http://localhost:8080/upload";

fs.createReadStream(file).pipe(request.post(url));

Here is the code on the server:

var express = require('express');
var app = express();
app.use(express.bodyParser());
var fs = require('fs');
var sys = require('sys');
app.listen(8080);


app.post('/upload', function (req, res) {
    console.log(req.files);
});

I want to eventually save the files received on the server. Right now when I run the service, then the client, the server logs an empty array - {} instead of the files uploaded.

How do I solve this?

Update: The server code works because I successfully tested it using Fiddler.

Gerson
  • 429
  • 2
  • 16
  • Your data will not be posted as `files`, but as raw body, read there http://stackoverflow.com/questions/17644836/get-rawbody-in-express as an example middleware to use for this purpose. – darma Nov 22 '13 at 18:52

2 Answers2

0

express.bodyParser() works for multipart/form-data encoded bodies. You are just sending file content as a body, so you need to pipe data to writeable stream or bufferize it.

vkurchatkin
  • 13,364
  • 2
  • 47
  • 55
0

For proper file uploads, you need to do a bit more work in your client:

var r = request.post(url);
var form = r.form();
form.append('my_file', fs.createReadStream(file));

(mostly taken from here)

robertklep
  • 198,204
  • 35
  • 394
  • 381