Categories
JavaScript Answers

How to use http.client in Node.js if there is basic authorization?

To use http.client in Node.js if there is basic authorization, we call request with the token in the header.

For instance, we write

const username = "Test";
const password = "123";
const auth =
  "Basic " + Buffer.from(username + ":" + password).toString("base64");
const header = { Host: "www.example.com", Authorization: auth };
const request = client.request("GET", "/", header);

to create the basic auth token.

Then we call client.request with the header object which has the Authorization header set to the basic auth token.

Categories
JavaScript Answers

How to send a message to a particular client with socket.io and JavaScript?

To send a message to a particular client with socket.io and JavaScript, we use the join method.

For instance, we write

const socket = io.connect("http://localhost");
socket.emit("join", { email: "user1@example.com" });

to emit the join event on client side.

Then we write

const io = require("socket.io").listen(80);

io.sockets.on("connection", (socket) => {
  socket.on("join", (data) => {
    socket.join(data.email);
  });
});

to listen to the join event with on and call join in the handler to add the client to the chat room.

Next, we write

io.sockets.in("user1@example.com").emit("new_msg", { msg: "hello" });

to call in to get the chat room and call emit to emit the new_msg event with the content on server side.

Then we write

socket.on("new_msg", (data) => {
  console.log(data.msg);
});

to listen to the new_msg on client with on.

Categories
JavaScript Answers

How to add wildcard routing to cover everything under and including a path with Node.js Express.js?

To add wildcard routing to cover everything under and including a path with Node.js Express.js, we can add multiple routes.

For instance, we write

const express = require("express");
const app = express.createServer();

const fooRoute = (req, res, next) => {
  res.end("Foo Route\n");
};

app.get("/foo*", fooRoute);
app.get("/foo", fooRoute);

app.listen(3000);

to add the /foo* route to handle any URLs that starts with /foo.

And we add the /foo route to handle request with URL /foo.

We call fooRoute to handle both kinds of requests.

Categories
JavaScript Answers

How to determine the installed Webpack version with JavaScript?

To determine the installed Webpack version with JavaScript, we use the --version option.

For instance, we run

webpack --version

to run webpack with the --version option to show the Webpack version.

We can shorten it to

webpack -v

Categories
JavaScript Answers

How to watch a folder for changes using Node.js, and print file paths when they are changed?

To watch a folder for changes using Node.js, and print file paths when they are changed, we use the chokidar package.

For instance, we write

const chokidar = require("chokidar");

const watcher = chokidar.watch(path, { ignored: /^\./, persistent: true });

watcher
  .on("add", (path) => {
    console.log("File", path, "has been added");
  })
  .on("change", (path) => {
    console.log("File", path, "has been changed");
  })
  .on("unlink", (path) => {
    console.log("File", path, "has been removed");
  })
  .on("error", (error) => {
    console.error("Error happened", error);
  });

to create a watcher with watch to watch a file or directory at the path for changes.

Then we call on to watch for additions by calling on with 'add'.

We watch for changes by calling on with 'change'.

And we watch for deletions by calling on with 'unlink'.

And we watch for any errors by calling on with 'error'.