Storing a image in mongodb

I'm trying to store images in mongo after downloading it with request

here is my code which causes a corrupted image to be stored in db.

request('http://test.jpg', function (error, response, image) {

    db.images.insert(

        {
            file_name: 'test.jpg',
            image: new Buffer(image)
        },

        function(err){

           //mongojs callback

        }
    );
});

Please note I am using mongojs module and storing the images in regular document as BinData type.

Also if I write the image to a file, read it then save the image to the database then there is no corruption. But I don't want to do this as is my intention to avoid the file-system altogether.

I'm pretty this has something to do with encoding or buffers but I don't know enough about these to solve my problem.

If you don't want to use a proper image storing solution like gridfs you could base64 encode your images.

How can you encode to Base64 using Javascript?

That gives you a string that you can store in mongo.