Categories
JavaScript Answers

How to create full path with Node’s fs.mkdirSync?

Sometimes, we want to create full path with Node’s fs.mkdirSync.

In this article, we’ll look at how to create full path with Node’s fs.mkdirSync.

How to create full path with Node’s fs.mkdirSync?

To create full path with Node’s fs.mkdirSync, we can use mkdirSync with recursive set to true.

For instance, we write

fs.mkdirSync(targetDir, {
  recursive: true
});

to create a directory at the targetDir path by setting recursive to true in the object in the 2nd argument.

Conclusion

To create full path with Node’s fs.mkdirSync, we can use mkdirSync with recursive set to true.

Categories
JavaScript Answers

How to remove documents using Node.js Mongoose?

Sometimes, we want to remove documents using Node.js Mongoose.

In this article, we’ll look at how to remove documents using Node.js Mongoose.

How to remove documents using Node.js Mongoose?

To remove documents using Node.js Mongoose, we can use the deleteOne or deleteMany method.

For instance, we write

TeleBot.deleteMany({
  chatID
}, (err, _) => {
  if (err) {
    return console.log(err);
  }
});

to call deleteMany to delete all entries with chatId equals chatId`.

The callback is run when deleteMany is done.

When there’re errors, err is set.

Conclusion

To remove documents using Node.js Mongoose, we can use the deleteOne or deleteMany method.

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.