0
votes

I have an image and a pdf in my file server. I have a node server which can access those two files from my file server this data will be transferred through an Rest API (octet-stream) to a react server where this information will be used on UI.As the API is being hit with same inputs i would like to reduce the time to retrieve the image and pdf from file server by doing server-side caching(using Redis). Previously I tried caching json data without any problem but when I am trying to cache an image and pdf in similar manner it is throwing error. I tried the code in this link, but I was not successful below is the sample code that I used

            app.post(
                '/file/:fileId', 
                function(req,res,next) {
                var
                //new instance of busboy
                busboy  = new Busboy({ headers: req.headers }),
                //where we will store our data
                fileData;
                
                //the 'file' event
                busboy.on('file', function(fieldname, file) {
                //the data event of the stream
                file.on('data', function(data) {
                    //setup the  fileData var if empty
                    if (!fileData) { fileData = data; } else {
                    //concat it to the first fileData
                    fileData.concat([data]);
                    }
                });
                //when the stream is done
                file.on('end', function(){
                    //set using redis
                    client.set(
                    rk('files',req.params.fileId),
                    fileData,
                    function(err, resp) {
                        if (err) { next(err); } else {
                        res.end(); //complete the http
                        }
                    }
                    );
                });
                });
                //let busboy handle the req stream
                req.pipe(busboy);
            }
            );


            app.get('/file/:fileId',function(req,res,next) {
            //grab it from file:[fileId]
            client.get(rk('file',req.params.fileId),function(err,value) {
                if (err) { next(err); } else {
                if (!value) {
                    next(); // no value means a next which is likely a 404
                } else {
                    res.setHeader('Content-Type','image/jpeg'); // set this to whatever you need or use some sort of mime type detection
                    res.end(value); //send the value and end the connection
                }
                }
            });
            })

            app.listen(3000)

This is the error throwing by node server

ReferenceError: Busboy is not defined
    at D:\rediscache-code\index.js:21:17
    at Layer.handle [as handle_request] (D:\rediscache-code\node_modules\express\lib\router\layer.js:95:5)
    at next (D:\rediscache-code\node_modules\express\lib\router\route.js:137:13)
    at Route.dispatch (D:\rediscache-code\node_modules\express\lib\router\route.js:112:3)
    at Layer.handle [as handle_request] (D:\rediscache-code\node_modules\express\lib\router\layer.js:95:5)
    at D:\rediscache-code\node_modules\express\lib\router\index.js:281:22
    at param (D:\rediscache-code\node_modules\express\lib\router\index.js:354:14)
    at param (D:\rediscache-code\node_modules\express\lib\router\index.js:365:14)
    at Function.process_params (D:\rediscache-code\node_modules\express\lib\router\index.js:410:3)
    at next (D:\rediscache-code\node_modules\express\lib\router\index.js:275:10)

Is this the correct way? or else is there any other possible ways to cache an image and pdf on server-side. If this is the correct way where did i go wrong?

1
Please define your use case and requirement. - Srinath Kamath
@SrinathKamath I updated use case and requirements - V Aruna

1 Answers

0
votes

I believe the problem is not Redis related. Have you included Busboy library?

Please try adding the require statement at the top of your Node file.

var Busboy = require('busboy');

Medium article you have linked does not include it, maybe that is the cause of your error.