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'.

Categories
JavaScript Answers

How to fix Node Sass does not yet support your current environment: Windows 64-bit with Unsupported runtime (88) error with JavaScript?

To fix Node Sass does not yet support your current environment: Windows 64-bit with Unsupported runtime (88) error with JavaScript, we rebuild node-sass.

For instance, we run

npm rebuild node-sass

to rebuild the node-sass package so that it runs on the computer’s current OS.

Categories
JavaScript Answers

How to display Node.js raw Buffer data as Hex string?

To display Node.js raw Buffer data as Hex string, we call the buffer toString method.

For instance, we write

const s = buff.toString("hex");

to call toString on the buff buffer to convert it to a hex string.