Categories
JavaScript Answers

How to render inline JavaScript with Jade or Pug?

Sometimes, we want to render inline JavaScript with Jade or Pug.

In this article, we’ll look at how to render inline JavaScript with Jade or Pug.

How to render inline JavaScript with Jade or Pug?

To render inline JavaScript with Jade or Pug, we can use script.

For instance, we write

script.
  if (usingJade)
    console.log('awesome')
  else
    console.log('use jade')

will be compiled to

<script>
  if (usingJade)
    console.log('awesome')
  else
    console.log('use jade')
</script>

Conclusion

To render inline JavaScript with Jade or Pug, we can use script.

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
Nodejs TypeScript Answers

How to extend Express request object using Typescript?

Sometimes, we want to extend Express request object using Typescript.

In this article, we’ll look at how to extend Express request object using Typescript.

How to extend Express request object using Typescript?

To extend Express request object using Typescript, we can declare our own type definition for the request object.

For instance, we write

custom.d.ts

declare namespace Express {
  export interface Request {
    tenant? : string
  }
}

to add the tenant property to the type definition of the request object.

Then we include custom.d.ts in tsconfig.json so the TypeScript compiler will pick up the type.

Finally, we can add the tenant property to the request without TypeScript errors like

router.use((req, res, next) => {
  req.tenant = 'tenant'
  next()
})

router.get('/who', (req, res) => {
  res.status(200).send(req.tenant)
})

Coclusion

To extend Express request object using Typescript, we can declare our own type definition for the request object.

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.