Categories
JavaScript Answers

How to read a file in Node.js?

Sometimes, we want to read a file in Node.js.

In this article, we’ll look at how to read a file in Node.js.

How to read a file in Node.js?

To read a file in Node.js, we can use the fs.readFile method.

For instance, we write

const fs = require('fs')
const path = require('path')
const filePath = path.join(__dirname, 'start.html');

fs.readFile(filePath, {
  encoding: 'utf-8'
}, (err, data) => {
  if (!err) {
    console.log(data);
  } else {
    console.log(err);
  }
});

to call fs.readFile with the path of the file to read, an object with encoding to read the file with the given encoding, and a callback that has the read file data when the file is read.

We read the file at filePath as a text file encoded with 'utf-8'.

And then we get the file data from data.

If err is set, that means there’s an error when reading the file.

Conclusion

To read a file in Node.js, we can use the fs.readFile method.

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.