8

I'm trying to just handle simple POST requests and append the data to a local file. However, when I try to POST raw text with postman, such as 'hi world', what's actually appended is [object Object]. I'm not sure what could be causing this if nothing should be interpreted as an object on either end. Thank you!

var express = require('express'),
    fs = require('fs')
    url = require('url');
var app = express();

app.configure(function(){
  app.use('/public', express.static(__dirname + '/public'));  
  app.use(express.static(__dirname + '/public')); 
  app.use(express.bodyParser());
});

app.post('/receive', function(request, respond) {
    filePath = __dirname + '/public/data.txt';
    fs.appendFile(filePath, request.body, function () {
        respond.end();
    });
});

app.listen(8080);
1
  • Since you're using bodyParser req.body is (I assume always) an object. I've got no idea how postman works but shouldn't any data you're trying to send be named (eg req.body.param)? Commented Jul 31, 2013 at 22:00

3 Answers 3

21
var express = require('express'),
    fs = require('fs'),
    url = require('url');
var app = express();

app.use('/public', express.static(__dirname + '/public'));  
app.use(express.static(__dirname + '/public')); 

app.post('/receive', function(request, respond) {
    var body = '';
    filePath = __dirname + '/public/data.txt';
    request.on('data', function(data) {
        body += data;
    });

    request.on('end', function (){
        fs.appendFile(filePath, body, function() {
            respond.end();
        });
    });
});

app.listen(8080);
Sign up to request clarification or add additional context in comments.

2 Comments

app.configure is now deprecated, but this code works if you comment out line 6 and 9.
Why not just request.pipe(fs.createWriteStream(filePath, {flags:'a'}))
6

If you want to do POST requests with regular urlencoded bodies, you don't want to use bodyParser (since you don't actually want to parse the body, you just want to stream it to the filesystem). Consider just streaming the chunks of data with req.pipe(writeableStreamToYourFile).

If you want to do file uploads, you can use bodyParser for that, but it handles multiple files and writes them to disk for you and you would need to iterate through req.files and copy them from a temporary directory to your target file.

1 Comment

This turned a 10-line function into a 1-liner. So much cruft, so little time... No need for busboy either.
4

If you want to store Json data then file should be of **.Json type. Otherwise try casting it into string and write into **.txt file. Like

var fs = require('fs');
var writer = fs.createWriteStream('output.txt');
response = {
name: '',
id: ''
}
writer.write(JSON.stringify(response));

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.