O'Reilly logo

Hands-On Microservices with Node.js by Diogo Resende

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Playing around with colors

Let's have some more fun and add a simple effect: greyscale. Let's add a parameter that will indicate to our service that the user wants the image in shades of grey:

app.param("greyscale", (req, res, next, greyscale) => {    if (greyscale != "bw") return next("route");    req.greyscale = true;    return next();});

Our parameter will only match the bw string. If it matches, it will mark a flag in the request object.

We can now change our image download function to handle this parameter, if defined:

function download_image(req, res) {    fs.access(req.localpath, fs.constants.R_OK , (err) => {        if (err) return res.status(404).end();        let image = sharp(req.localpath);        if (req.width && req.height) {            image.ignoreAspectRatio();        } if ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required