Categories
JavaScript Answers

How to make a remote REST call with Node.js?

Sometimes, we want to make a remote REST call with Node.js.

In this article, we’ll look at how to make a remote REST call with Node.js.

How to make a remote REST call with Node.js?

To make a remote REST call with Node.js, we can use axios.

We install it by running

npm i axios

Then we use it by writing

const url = `https://example.com`;

const {
  data
} = await axios({
  method: 'get',
  url,
})

to call axios with the 'get' request method and the request url.

It returns a promise with the response data stored in data.

Conclusion

To make a remote REST call inside Node.js, we can use axios.

Categories
JavaScript Answers

How to create folder or use existing with Node.js?

Sometimes, we want to create folder or use existing with Node.js.

In this article, we’ll look at how to create folder or use existing with Node.js.

How to create folder or use existing with Node.js?

To create folder or use existing with Node.js, we can use the recursive option with mkdirSync or mkdir.

For instance, we write

fs.mkdirSync(dirpath, {
  recursive: true
})

to create the folder at the path given by dirpath if it doesn’t exists by setting recursive to true with mkdirSync.

mkdirSync creates the folder synchronously.

We can create a folder asynchronously with mkdir by writing

await fs.promises.mkdir(dirpath, {
  recursive: true
})

Conclusion

To create folder or use existing with Node.js, we can use the recursive option with mkdirSync or mkdir.

Categories
JavaScript Answers

How to save a base64-encoded image to disk with Node.js?

Sometimes, we want to save a base64-encoded image to disk with Node.js.

In this article, we’ll look at how to save a base64-encoded image to disk with Node.js.

How to save a base64-encoded image to disk with Node.js?

To save a base64-encoded image to disk with Node.js, we can use the fs.writeFile method.

For instance, we write:

const base64Data = str.replace(/^data:image\/png;base64,/, "");

const fs = require("fs")

fs.writeFile("out.png", base64Data, 'base64', (err) => {
  console.log(err);
});

to remove the MIME type prefix from the base64 string with string replace.

Then we call fs.writeFile with the path to write the file to, the base64data string, the 'base64' encoding, and a callback that runs when writeFile is done.

If there’s an error, err will be set.

We need to pass in 'base64' to write the file as a base64 encoded file.

Conclusion

To save a base64-encoded image to disk with Node.js, we can use the fs.writeFile method.

Categories
JavaScript Answers

How to read file with async and await in Node.js?

Sometimes, we want to read file with async and await in Node.js.

In this article, we’ll look at how to read file with async and await in Node.js.

How to read file with async and await in Node.js?

To read file with async and await in Node.js, we can use the promise version of fs.readFile.

For instance, we write

const fs = require('fs').promises;

const read = async () => {
  const data = await fs.readFile("monolitic.txt", "binary");
  return new Buffer(data);
}

to define the read function that calls fs.readFile with fs returned from the promises property of the fs module.

This version of readFile will return a promise.

It takes the file path and the encoding of the file.

And we get the file data from the promise’s resolve value.

Therefore, data has the file data.

Conclusion

To read file with async and await in Node.js, we can use the promise version of fs.readFile.

Categories
JavaScript Answers

How to specify HTTP error code using Express.js?

Sometimes, we want to specify HTTP error code using Express.js.

In this article, we’ll look at how to specify HTTP error code using Express.js.

How to specify HTTP error code using Express.js?

To specify HTTP error code using Express.js, we can use the res.status method.

For instance, we write

res.status(400);
res.send('error');

to call res.status with 400 to return a 400 response.

We also call res.send with the error message string to return an error message in the response body.

Conclusion

To specify HTTP error code using Express.js, we can use the res.status method.