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.

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.