Categories
JavaScript Answers

How to get local IP address in Node.js?

Sometimes, we want to get local IP address in Node.js.

In this article, we’ll look at how to get local IP address in Node.js.

How to get local IP address in Node.js?

To get local IP address in Node.js, we can use the os.networkInterfaces method.

For instance, we write

const {
  networkInterfaces
} = require('os');

const nets = networkInterfaces();
const results = Object.create(null);

for (const name of Object.keys(nets)) {
  for (const net of nets[name]) {
    if (net.family === 'IPv4' && !net.internal) {
      if (!results[name]) {
        results[name] = [];
      }
      results[name].push(net.address);
    }
  }
}

to call networkInterfaces to return an object with some network data.

Then we loop through the nets[name] properties to get the IPv4 address from the nets object.

We use !net.internal to ignore network addresses that are local.

Conclusion

To get local IP address in Node.js, we can use the os.networkInterfaces method.

Categories
JavaScript Answers

How to turn a Mongoose document into a plain object?

Sometimes, we want to turn a Mongoose document into a plain object.

In this article, we’ll look at how to turn a Mongoose document into a plain object.

How to turn a Mongoose document into a plain object?

To turn a Mongoose document into a plain object, we can use the lean method.

For instance, we write

MyModel.findOne().lean().exec((err, doc) => {
  doc.addedProperty = 'foobar';
});

to call lean on the document returned by findOne.

Then we get the plain object version of the document from doc in the callback that we call exec with.

Conclusion

To turn a Mongoose document into a plain object, we can use the lean method.

Categories
JavaScript Answers

How to compare Mongoose _id and strings?

Sometimes, we want to compare Mongoose _id and strings.

In this article, we’ll look at how to compare Mongoose _id and strings.

How to compare Mongoose _id and strings?

To compare Mongoose _id and strings, we can convert the _id to a string with the toString method.

For instance, we write

results.userId === AnotherMongoDocument._id.toString()

to convert the _id property of AnotherMongoDocument to a string with toString.

We can also use the equals method of a Mongoose document object to do the same comparison.

For instance, we write

results.userId.equals(AnotherMongoDocument._id)

to call equals with the AnotherMongoDocument._id property.

Conclusion

To compare Mongoose _id and strings, we can convert the _id to a string with the toString method.

Categories
JavaScript Answers

How to pass variables to the next middleware using next() in Express.js?

Sometimes, we want to pass variables to the next middleware using next() in Express.js.

In this article, we’ll look at how to pass variables to the next middleware using next() in Express.js.

How to pass variables to the next middleware using next() in Express.js?

To pass variables to the next middleware using next() in Express.js, we can add the data to the res object.

For instance, we write

app.use((req, res, next) => {
  res.locals.user = req.user;
  res.locals.authenticated = !req.user.anonymous;
  next();
});

app.use((req, res, next) => {
  if (res.locals.authenticated) {
    console.log(res.locals.user.id);
  }
  next();
});

to set res.locals to some values in the first middleware.

And then we call next to call the next middleware function.

Then in the 2nd middleware, we check the values that we set as properties of res.

Conclusion

To pass variables to the next middleware using next() in Express.js, we can add the data to the res object.

Categories
JavaScript Answers

How to find home directory in platform agnostic way with Node.js?

Sometimes, we want to find home directory in platform agnostic way with Node.js.

In this article, we’ll look at how to find home directory in platform agnostic way with Node.js.

How to find home directory in platform agnostic way with Node.js?

To find home directory in platform agnostic way with Node.js, we can use the os.homedir method.

For instance, we write

const os = require('os');

console.log(os.homedir());

to call os.homedir to return the home directory path as a string.

Conclusion

To find home directory in platform agnostic way with Node.js, we can use the os.homedir method.