Categories
JavaScript Answers

How to watch a folder for changes and print file paths when they are changed with Node.js?

Sometimes, we want to watch a folder for changes and print file paths when they are changed with Node.js.

In this article, we’ll look at how to watch a folder for changes and print file paths when they are changed with Node.js.

How to watch a folder for changes and print file paths when they are changed with Node.js?

To watch a folder for changes and print file paths when they are changed with Node.js, we can use the chokidar package.

To install it, we run

npm i chokidar

then we use it by writing

const chokidar = require('chokidar');

const watcher = chokidar.watch('file or dir', {
  ignored: /^\./,
  persistent: true
});

watcher
  .on('add', (path) => {
    console.log('File', path, 'has been added');
  })
  .on('change', (path) => {
    console.log('File', path, 'has been changed');
  })
  .on('unlink', (path) => {
    console.log('File', path, 'has been removed');
  })
  .on('error', (error) => {
    console.error('Error happened', error);
  })

to create a watcher that watches for 'file or dir' changes.

And then we call watcher.on with various event names and their event handlers.

The 'change' event is triggered when files are changed in the folders being watched.

Conclusion

To watch a folder for changes and print file paths when they are changed with Node.js, we can use the chokidar package.

Categories
JavaScript Answers

How to read all files in a directory, store them in objects, and send the object with Node.js?

Sometimes, we want to read all files in a directory, store them in objects, and send the object with Node.js.

In this article, we’ll look at how to read all files in a directory, store them in objects, and send the object with Node.js.

How to read all files in a directory, store them in objects, and send the object with Node.js?

To read all files in a directory, store them in objects, and send the object with Node.js, we can use the readdir and readFile methods.

For instance, we write

const fs = require('fs');

fs.readdir(dirname, (err, filenames) => {
  if (err) {
    onError(err);
    return;
  }
  filenames.forEach((filename) => {
    fs.readFile(dirname + filename, 'utf-8', (err, content) => {
      if (err) {
        onError(err);
        return;
      }
      onFileContent(filename, content);
    });
  });
});

to call fs.readdir to read the file paths from the dirname folder.

Then we get the filenames array from the callback parameter.

And then we call filenames.forEach with a callback that calls fs.readFile to read file with path dirname + filename.

And we get the content of the file from the content parameter in the readFile callback.

Conclusion

To read all files in a directory, store them in objects, and send the object with Node.js, we can use the readdir and readFile methods.

Categories
JavaScript Answers

How to populate after save with Mongoose?

Sometimes, we want to populate after save with Mongoose.

In this article, we’ll look at how to populate after save with Mongoose.

How to populate after save with Mongoose?

To populate after save with Mongoose, we can use the populate and execPopulate method after calling save.

For instance, we write

const t = new MyModel(value)
await t.save()
t.populate('my-path').execPopulate()

to call t.save to save t in the database.

Then we call t.populate with the property to populate.

And then we call execPopulate to run the populate operation.

Conclusion

To populate after save with Mongoose, we can use the populate and execPopulate method after calling save.

Categories
JavaScript Answers

How to get an accurate timestamp in Node.js?

Sometimes, we want to get an accurate timestamp in Node.js.

In this article, we’ll look at how to get an accurate timestamp in Node.js.

How to get an accurate timestamp in Node.js?

To get an accurate timestamp in Node.js, we can use the process.hrtime.bigint method.

For instance, we write

process.hrtime.bigint()

to call process.hrtime.bigint to return the current timestamp in nanoseconds as a JavaScript bigint.

Conclusion

To get an accurate timestamp in Node.js, we can use the process.hrtime.bigint method.

Categories
JavaScript Answers

How to drop a database with Mongoose?

Sometimes, we want to drop a database with Mongoose.

In this article, we’ll look at how to drop a database with Mongoose.

How to drop a database with Mongoose?

To drop a database with Mongoose, we can remove all the content of a collection.

For instance, we write

Model.remove({}, (err) => {
  console.log('collection removed')
});

to call Model.remove with an empty object to remove all items in the collection that’s mapped to Model.

The callback is run when all the items are removed from the collection.

Conclusion

To drop a database with Mongoose, we can remove all the content of a collection.