I\'m trying to send binary data to an express app. It works fine, as long as my values are smaller than 0x80. If a single value is 0x80 or greater, it messes up the whole b
After struggling with this for way too long, I came up with this solution:
var express = require('express');
var bodyParser = require('body-parser');
var fs = require('fs');
var path = require('path');
app.use(bodyParser.raw({type: 'application/octet-stream', limit : '2mb'}))
app.post('/nist-ws/rest/v1/nist/', function(req, res) {
var filePath = path.join(__dirname, 'nist_received', `/${Date.now()}.nist`)
fs.open(filePath, 'w', function(err, fd) {
fs.write(fd, req.body, 0, req.body.length, null, function(err) {
if (err) throw 'error writing file: ' + err;
fs.close(fd, function() {
console.log('wrote the file successfully');
res.status(200).end();
});
});
});
});
bodyParser.raw fills req.body with a Buffer and the rest of the code is from: https://stackabuse.com/writing-to-files-in-node-js/
It turned out to be an encoding issue. Everything seems to default to 'utf8', but in this case it needs to be set to 'binary'.
For example:
var buf = new Buffer(body.toString('binary'),'binary');
Equally as important, I needed to set nodejs multiparty parser's encoding to 'binary'. See: https://github.com/superjoe30/node-multiparty/issues/37