Categories
JavaScript Answers

How to convert Mongoose docs to JSON with Node.js?

To convert Mongoose docs to JSON with Node.js, we use the lean method.

For instance, we write

UserModel.find()
  .lean()
  .exec((err, users) => {
    return res.end(JSON.stringify(users));
  });

to call lean to convert the results returned from find to a plain object.

Then we get the results from the users parameter in the exec callback.

users is a plain JavaScript array.

Categories
JavaScript Answers

How to extend TypeScript Global object in node.js?

To extend TypeScript Global object in node.js, we add a type declaration file.

For instance, we write

declare module NodeJS {
  interface Global {
    spotConfig: any;
  }
}

to add a type declaration in vendor.d.ts to add the Global interface into the project type definition with the properties we want to add to the global interface.

Therefore, we can use the spotConfig global variable without type errors.

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"
  }
  //...
}