Categories
JavaScript Answers

How to get all directories within directory with Node.js?

Sometimes, we want to get all directories within directory with Node.js.

In this article, we’ll look at how to get all directories within directory with Node.js.

How to get all directories within directory with Node.js?

To get all directories within directory with Node.js, we can use the readdir method.

For instance, we write

const {
  promises: {
    readdir
  }
} = require('fs')

const getDirectories = async source => {
  const dirs = await readdir(source, {
    withFileTypes: true
  })
  return dirs
    .filter(dirent => dirent.isDirectory())
    .map(dirent => dirent.name)
}

to create the getDirections function that takes the source path string.

In it, we call readdir with source and an object that has withFileTypes set to true to return the file types with the items.

Then we call isDirectory to return an array of directories in the source folder and call map to return the names of each folder.

Conclusion

To get all directories within directory with Node.js, we can use the readdir method.

Categories
JavaScript Answers

How to get remote client address with Node.js and Express.js?

Sometimes, we want to get remote client address with Node.js and Express.js.

In this article, we’ll look at how to get remote client address with Node.js and Express.js.

How to get remote client address with Node.js and Express.js?

To get remote client address with Node.js and Express.js, we can use the req.headers or req.socket.remoteAddress properties.

For instance, we write

const ip = req.headers['x-forwarded-for'] || req.socket.remoteAddress

to get the client IP address from the x-forwarded-for request header with req.headers['x-forwarded-for'].

If that’s not set, then we use req.socket.remoteAddress to get the client IP address.

Conclusion

To get remote client address with Node.js and Express.js, we can use the req.headers or req.socket.remoteAddress properties.

Categories
JavaScript Answers

How to use async/await at the top level with Node.js?

Sometimes, we want to use async/await at the top level with Node.js.

In this article, we’ll look at how to use async/await at the top level with Node.js.

How to use async/await at the top level with Node.js?

To use async/await at the top level with Node.js, we can create an IIFE.

For instance, we write

(async () => {
  try {
    const text = await main();
    console.log(text);
  } catch (e) {
    // ...
  }
})();

to call the async function immediately after it’s been created by wrapping the function with parentheses and call it with () at the end.

We use await before calling main to return the resolved value of the promise returned by main.

And we use catch to catch any promise rejecttion.

Conclusion

To use async/await at the top level with Node.js, we can create an IIFE.

Categories
JavaScript Answers

How to determine the current operating system with Node.js?

Sometimes, we want to determine the current operating system with Node.js.

In this article, we’ll look at how to determine the current operating system with Node.js.

How to determine the current operating system with Node.js?

To determine the current operating system with Node.js, we can use the process.platform property.

For instance, we write

const isWin = process.platform === "win32";

to check if the Node.js program is running on Windows.

All possible values for process.platform include:

  • aix
  • darwin
  • freebsd
  • linux
  • openbsd
  • sunos
  • win32
  • android

Conclusion

To determine the current operating system with Node.js, we can use the process.platform property.

Categories
JavaScript Answers

How to fix Node.js throwing “btoa is not defined” error?

Sometimes, we want to fix Node.js throwing the "btoa is not defined" error.

In this article, we’ll look at how to fix Node.js throwing the "btoa is not defined" error.

How to fix Node.js throwing "btoa is not defined" error?

To fix Node.js throwing the "btoa is not defined" error, we can use the Buffer.from method with toString instead of btoa.

For instance, we write

console.log(Buffer.from('Hello World').toString('base64'));

to call Buffer.from with the string that we want to convert to base64.

Then we call toString with 'base64' to convert it to a base64 string.

We can decode it with

console.log(Buffer.from(b64Encoded, 'base64').toString());

where b64Encoded is the base64 string to decode.

Conclusion

To fix Node.js throwing the "btoa is not defined" error, we can use the Buffer.from method with toString instead of btoa.