Categories
JavaScript Answers

How to close a readable stream before end with Node.js?

To close a readable stream before end with Node.js, we call the destroy method.

For instance, we write

const fs = require("fs");

const readStream = fs.createReadStream("lines.txt");
readStream
  .on("data", (chunk) => {
    console.log(chunk);
    readStream.destroy();
  })
  .on("end", () => {
    console.log("All the data in the file has been read");
  })
  .on("close", (err) => {
    console.log("Stream has been destroyed and file has been closed");
  });

to create the readStream with createReadStream.

Then we call destroy in the data event handler to close the read stream.

Categories
JavaScript Answers

How to stub a class method with Sinon.js and JavaScript?

To stub a class method with Sinon.js and JavaScript, we call the stub method.

For instance, we write

sinon.stub(YourClass.prototype, "myMethod").callsFake(() => {
  return {};
});

to call stub with YourClass.prototype to stub the myMethod instance method on YourClass.

We call callFake with the mock function for the myMethod method.

Likewise, for static class methods, we write

sinon.stub(YourClass, "myStaticMethod").callsFake(() => {
  return {};
});

to mock the YourClass.myStaticMethod static method with stub.

Categories
JavaScript Answers

How to fix passport.js passport.initialize() middleware not in use with Node.js?

To fix passport.js passport.initialize() middleware not in use with Node.js, we call app.use with the middleware returned by initialize.

For instance, we write

app.use(
  cookieSession({
    maxAge: 30 * 24 * 60 * 60 * 1000,
    keys: [keys.cookieKey],
  })
);
app.use(passport.initialize());
app.use(passport.session());

to call app.use with the function returned by initialize to add Passport to our Express app.

We call cookieSession to return a middleware to let us use cookie sessions.

Categories
JavaScript Answers

How to use Morgan logger with Node.js?

To use Morgan logger with Node.js, we use it as a middleware.

For instance, we write

const express = require("express");
const app = express();
const morgan = require("morgan");

if (app.get("env") === "production") {
  app.use(
    morgan("common", {
      skip: (req, res) => {
        return res.statusCode < 400;
      },
      stream: __dirname + "/../morgan.log",
    })
  );
} else {
  app.use(morgan("dev"));
}

to call morgan with the logger options to create a middleware function for logging.

We set stream to the path of the log file to write to.

And we set skip to a function that returns the condition of the items to skip in the log.

Then we call app.use to use the middleware in our Express app.

Categories
JavaScript Answers

How to restore/reset npm configuration to default values with JavaScript?

To restore/reset npm configuration to default values with JavaScript, we run npm config delete.

For instance, we run

npm config delete registry

to delete the registry entry from the config to make npm use the default value for the registry config.