0

Here is my server code. I want to show the resized images from the output folder, And here resize is a custom js created by me just to take image parameters and resize it. After resizing I want to show the image from the output folder.

Folder Structure

const express = require('express');
const files = require('fs');
const resize = require('./resize.js');

const server = express();
const port = 200;
server.get('/', function (req, res) {
  const PicName = req.query.name;
  const widthString = req.query.width;
  const heightString = req.query.height;
  const PicFormat = req.query.format;
  let width;
  let height;
  if (widthString) {
    width = parseInt(widthString);
    if (width < 1) {
      res.send('<h1>Error in parameter</h1>');
      return;
    }
  }
  if (heightString) {
    height = parseInt(heightString);
    if (height < 1) {
      res.send('<h1>Error in parameter</h1>');
      return;
    }
  }
  if (PicFormat === 'gif') {
    res.send('<h1>Error in format</h1>');
    return;
  }
  files.access(`./images/${PicName}`, files.constants.F_OK, function (err) {
    if (err) {
      res.send('<h1>Image does not Exist</h1>');
      return;
    }
    res.type(`image/${PicFormat || 'jpg'}`);

    resize(`./images/${PicName}`, PicFormat, width, height).toFile(
      `./output/${PicName}`
    );
    resize(`./images/${PicName}`, PicFormat, width, height).pipe(res);
  });
});
server.listen(port, function () {
  console.log(`Example app listening at http://localhost:${port}`);
});
2
  • Does this answer your question? nodejs - How to read and output jpg image? Commented Jan 31, 2021 at 4:02
  • What i wanted to know is in my code resize(./images/${PicName}, PicFormat, width, height).toFile( ./output/${PicName} ); resize(./images/${PicName}, PicFormat, width, height).pipe(res); }); I have to do the resizing repeatedly to show the image. I want to do is resize once and show the image from the resized image folder to the localhost Commented Jan 31, 2021 at 4:26

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.