Categories
Functional Javascript

How to do image resizing without ImageMagick with Node.js?

To do image resizing without ImageMagick with Node.js, we use the lwip package.

To use it we write

require("lwip").open("image.jpg", (err, image) => {
  image
    .batch()
    .scale(0.75)
    .rotate(45, "white")
    .crop(200)
    .blur(5)
    .writeFile("output.jpg", (err) => {
      //...
    });
});

to call open to open image.jpg for resizing.

Then we call scale to resize it to 75% its original size.

We rotate by 45 degrees clockwise with rotate and fill the space with white.

Then we call crop to crop it to 200px by 200px.

Next we call blur to blur the image.

And we call writeFile to write the modified image to output.jpg.

We can get errors from err in the callback.

Categories
JavaScript Answers

How to set multiple file entry and output in project with webpack and JavaScript?

To set multiple file entry and output in project with webpack and JavaScript, we set the entry and output property.

For instance, in webpack.config.js, we write

module.exports = (env, options) => ({
  //...
  entry: {
    "dir1/js/bundle": path.resolve(__dirname, "/apps/dir1/js/main.js"),
    "dir2/foo": path.resolve(__dirname, "/apps/dir2/index.js"),
  },
  output: {
    path: path.resolve(__dirname, "/apps"),
    filename: "[name].js",
  },
  //...
});

to add the entry property and set it to an object with the paths of the entry point files.

And we set the output property to an object with the path to output the files by setting the path property.

We set filename to the output file name.

Categories
JavaScript Answers

How to generate timestamp unix epoch format with Node.js?

To generate timestamp unix epoch format with Node.js, we use the Date constructor.

For instance, we write

const ts = Math.floor(new Date().getTime() / 1000);

to create a new date with the current date and time with the Date constructor.

Then we call getTime to get its timestamp in milliseconds.

And then we divide that by 1000 to get the timestamp in seconds.

Finally, we call Math.floor with the timestamp in seconds to round it down to the nearest integer.

Categories
JavaScript Answers

How to chain multiple pieces of middleware for specific route in Node.js Express?

To chain multiple pieces of middleware for specific route in Node.js Express, we put the middlewares in an array.

For instance, we write

const middleware = {
  requireAuthentication: (req, res, next) => {
    console.log("private route list!");
    next();
  },
  logger: (req, res, next) => {
    console.log("Original request hit : " + req.originalUrl);
    next();
  },
};

app.get(
  "/",
  [middleware.requireAuthentication, middleware.logger],
  (req, res) => {
    res.send("Hello!");
  }
);

to add the middlewares into the middleware object.

Then we call app.get with an array with the middlewares as the 2nd argument to call them in the same order they’re listed before calling the route handler.

Categories
JavaScript Answers

How to use a variable as a field name in mongodb-native findOne() with JavaScript?

To use a variable as a field name in mongodb-native findOne() with JavaScript, we call use a computed property name.

For instance, we write

const name = req.params.name;
const value = req.params.value;
collection.findOne({ [name]: value }, (err, item) => {
  res.send(item);
});

to call findOne with an object that has the name value as the property name.

Then we get the result from item in the callback.