I have a front end webserver Node + Express that receives an uploaded file from a browser. I then want to get the file data and send it via amqp to a worker. This is my code for accepting the upload and sending the file over amqp :
adminCommercialRegistration : function (req,res,next) {
if(!this.checkLogin (req,res)) return;
fs.readFile(req.files.html.path, function (err,data) {
if(err)
{
res.json('Error reading uploaded file');
}
else
{
var metaData = {
companyName : req.body.companyName,
date : req.body.date
};
var payLoad = { metaData : metaData, data : data };
var connection = require('amqp').createConnection({ host : '172.27.164.28'});
connection.on('ready', function(){
console.log('Connected to ' + connection.serverProperties.product);
var exchange = connection.exchange('ooparoopa');
var queue = connection.queue('htmlFileUpload');
queue.on('queueDeclareOk', function(args) {
console.log('Queue Opened');
queue.bind(exchange, '#html');
queue.on('queueBindOk',function (){
console.log('Queue Bound');
exchange.publish('#html', { payLoad : payLoad });
res.redirect('/commercial');
});
});
});
}
});
},
This part works fine and will send the data. I have inspected the data object on this side and it is a Buffer.
The code below is the worker process. It receives the data and adds the required information to the database. However, there is a problem with the file data. It doesn't look like it is a buffer. And I get the error message unknown encoding. What must I do to get this back into a binary buffer?
connection.on('ready', function(){
console.log('Connected to ' + connection.serverProperties.product);
var exchange = connection.exchange('ooparoopa');
var queue = connection.queue('htmlFileUpload');
queue.on('queueDeclareOk', function(args){
queue.bind(exchange, '#html');
queue.subscribe({ack:true}, function (payLoad) {
queue.shift();
console.log('Message Received:');
console.log(payLoad.payLoad.metaData.companyName);
var Commercial = db.model('Commercial');
var newCommercial = new Commercial({ companyName : payLoad.payLoad.metaData.companyName, date : payLoad.payLoad.metaData.date });
newCommercial.save(function (err,newComm){
if(err)
{
console.log(err);
}
else
{
console.log(payLoad.payLoad.data.length)
var folder = '/var/www/ooparoopastatic/html/';
var _id = newComm.id;
var filename = _id + '.zip';
console.log(payLoad.payLoad.data);
fs.writeFileSync(folder + filename, buffer, function (err) {
if(err)
{
console.log(err);
}
else
{
console.log('Writing to file at:' + folder + filename);
}
});
}
});
});
});
})