Categories
JavaScript Answers

How to read from stdin line by line in Node?

Sometimes, we want to read from stdin line by line in Node.

In this article, we’ll look at how to read from stdin line by line in Node.

How to read from stdin line by line in Node?

To read from stdin line by line in Node, we can use the readline module.

To use it, we write

const readline = require('readline');
const rl = readline.createInterface({
  input: process.stdin,
  output: process.stdout,
  terminal: false
});

rl.on('line', (line) => {
  console.log(line);
})

to call readline.createInterface to return an object that lets us read the stdin by setting input to process.stdin.

Then when stdin updates, the 'line' event is emitted and we listen to the update with

rl.on('line', (line) => {
  console.log(line);
})

line has the stdin input value

Conclusion

To read from stdin line by line in Node, we can use the readline module.

Categories
JavaScript Answers

How to get all registered routes in Express?

Sometimes, we want to get all registered routes in Express.

In this article, we’ll look at how to get all registered routes in Express.

How to get all registered routes in Express?

To get all registered routes in Express, we can use the app._router.stack property.

For instance, we write

app._router.stack.forEach((r) => {
  if (r?.route?.path){
    console.log(r?.route?.path)
  }
})

to loop through the array returned by app._router.stack, which has all the routes in an array.

We get the path of each route from r.route.path.

Conclusion

To get all registered routes in Express, we can use the app._router.stack property.

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.