Categories
JavaScript Answers

How to include JavaScript class definition from another file in Node.js?

To include JavaScript class definition from another file in Node.js, we set the class as the value of module.exports.

For instance, in user.js, we write

class User {
  //...
}

module.exports = User;

to export the User class.

Then in app.js, we write

const User = require("./user.js");

let user = new User();

to import the user.js file with required.

And then we create a User object.

Categories
JavaScript Answers

How to fix Mongoose’s find method with $or condition does not work properly with JavaScript?

To fix Mongoose’s find method with $or condition does not work properly with JavaScript, we set $or to an array with the conditions we’re searching for.

For instance, we write

const ObjectId = require("mongoose").Types.ObjectId;
const objId = new ObjectId(param.length < 12 ? "123456789012" : param);

User.find(
  { $or: [{ _id: objId }, { name: param }, { nickname: param }] },
  (err, docs) => {
    if (!err) res.send(docs);
  }
);

to call find with an object with $or set to an object with the conditions we’re looking for.

We get the results from the docs parameter in the callback.

Categories
JavaScript Answers

How to get the client’s IP address in socket.io and JavaScript?

To get the client’s IP address in socket.io and JavaScript, we use the handshake.address property.

For instance, we write

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

io.sockets.on("connection", (socket) => {
  const address = socket.handshake.address;
  console.log(address.address, address.port);
});

to listen for the connection event with on.

We get the client address from socket.handshake.address.

And we get the IP address and port from the returned address.

Categories
JavaScript Answers

How to promisify Node’s child_process.exec and child_process.execFile functions with Node.js Bluebird?

To promisify Node’s child_process.exec and child_process.execFile functions with Node.js Bluebird, we use the util.promisfy method.

For instance, we write

const util = require("util");
const exec = util.promisify(require("child_process").exec);

const run = async () => {
  try {
    const { stdout, stderr } = await exec("ls");
    console.log("stdout:", stdout);
    console.log("stderr:", stderr);
  } catch (e) {
    console.error(e);
  }
};

to call util.promisify with the child_process module’s exec method to convert it to return a promise.

Then we define the run function that calls exec to run a command.

It returns a promise, so we await to get an object with the stdout and stderr outputs.

This works with Node.js 10 or later.

Categories
JavaScript Answers

How to fix nodemon command is not recognized in terminal for Node.js server?

To fix nodemon command is not recognized in terminal for Node.js server, we put nodemon in a script in package.json.

For instance, in package.json, we write something like

{
  //....
  "scripts": {
    "server": "nodemon server.js"
  }
  //....
}

to run server.js with nodemon.

Then we run

npm run server

to run the script.

We install nodemon with

npm i nodemon