Categories
JavaScript Answers

How to pipe the same readable stream into multiple (writable) targets with Node.js?

To pipe the same readable stream into multiple (writable) targets with Node.js, we call the pipe method on each stream.

For instance, we write

const spawn = require("child_process").spawn;
const PassThrough = require("stream").PassThrough;

const a = spawn("echo", ["hi user"]);
const b = new PassThrough();
const c = new PassThrough();

a.stdout.pipe(b);
a.stdout.pipe(c);

let count = 0;
b.on("data", (chunk) => {
  count += chunk.length;
});
b.on("end", () => {
  console.log(count);
  c.pipe(process.stdout);
});

to create 2 PassThrough objects.

Then we call pipe to pipe our input from a to to the PassThrough objects with pipe to write to them.

We get the data written from the 'data' event handler’s chunk parameter.

Categories
JavaScript Answers

How to send additional data on socket connection with JavaScript socket.io?

To send additional data on socket connection with JavaScript socket.io, we call the emit function with the data.

For instance, we write

socket.on("connect", () => {
  socket.emit("hello", data);
});

io.on("connection", (client) => {
  client.on("hello", (data) => {});
});

to call emit to emit the 'hello' event with the data we want to send to the client.

Categories
JavaScript Answers

How to run “npm start” with a specific browser with create-react-app?

To run "npm start" with a specific browser with create-react-app, we set the BROWSER environment variable.

For instance, in package.json, we write

{
  //...
  "scripts": {
    "start": "BROWSER='chrome' react-scripts start"
  }
  //...
}

to set the BROWSER environment variable to 'chrome'.

And then we run react-scripts start to start create-react-app with Chrome.

This works on windows.

On Linux, we write

{
  //...
  "scripts": {
    "start": "BROWSER='google-chrome-stable' react-scripts start"
  }
  //...
}

And on Mac OS, we write

{
  //...
  "scripts": {
    "start": "BROWSER='google chrome' react-scripts start"
  }
  //...
}
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.