Categories
JavaScript Answers

How to write file if parent folder doesn’t exist with Node.js?

Sometimes, we want to write file if parent folder doesn’t exist with Node.js.

In this article, we’ll look at how to write file if parent folder doesn’t exist with Node.js.

How to write file if parent folder doesn’t exist with Node.js?

To write file if parent folder doesn’t exist with Node.js, we can call mkdir to create the folder before creating the file in the folder.

For instance, we write

const fs = require('fs');
const getDirName = require('path').dirname;

fs.mkdir(getDirName(path), {
  recursive: true
}, (err) => {
  if (err) return cb(err);

  fs.writeFile(path, contents, (err) => {
    //...
  });
});

to call fs.mkdir with the folder path we want to create and an object that has recursive set to true to create the folder event if the parent folder doesn’t exist.

Then in the mkdir callback, we call writeFile to write the file to the given path.

Conclusion

To write file if parent folder doesn’t exist with Node.js, we can call mkdir to create the folder before creating the file in the folder.

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.