Categories
JavaScript Answers

How to convert an existing callback API to promises with Node.js?

Sometimes, we want to convert an existing callback API to promises with Node.js.

In this article, we’ll look at how to convert an existing callback API to promises with Node.js.

How to convert an existing callback API to promises with Node.js?

To convert an existing callback API to promises with Node.js, we can use the Promise constructor.

For instance, we write

const load = (param) => {
  return new Promise((resolve, reject) => {
    getStuff(param, (err, data) => {
      if (err !== null) {
        reject(err);
      } else {
        resolve(data);
      }
    });
  });
}

to create the load function that returns a promise that we create with the Promise constructor.

If there’s an error, err is set to a value, so we call reject with the err object.

Otherwise, we call resolve with the data that we get when the getStuff function succesfully runs.

Conclusion

To convert an existing callback API to promises with Node.js, we can use the Promise constructor.

Categories
JavaScript Answers

How to print to the console without a trailing newline with Node.js?

Sometimes, we want to print to the console without a trailing newline with Node.js.

In this article, we’ll look at how to print to the console without a trailing newline with Node.js.

How to print to the console without a trailing newline with Node.js?

To print to the console without a trailing newline with Node.js, we can use the process.stdout.write method.

For instance, we write

process.stdout.write(`Downloading ${data.length} bytes\r`);

to print Downloading ${data.length} bytes\r.

The \r character lets us overwrite the last line that was printed with new text that we print with process.stdout.write.

Conclusion

To print to the console without a trailing newline with Node.js, we can use the process.stdout.write method.

Categories
JavaScript Answers

How to do base64 encoding in Node.js?

Sometimes, we want to do base64 encoding in Node.js.

In this article, we’ll look at how to do base64 encoding in Node.js.

How to do base64 encoding in Node.js?

To do base64 encoding in Node.js, we can use the Buffer.from method.

For instance, we write

const b = Buffer.from("Hello World").toString('base64')

to call Buffer.from with the string we want to encode.

Then we convert the returned buffer object to a base64 string by calling toString with 'base64'.

Conclusion

To do base64 encoding in Node.js, we can use the Buffer.from method.

Categories
JavaScript Answers

How to make a HTTP POST request in Node.js?

Sometimes, we want to make a HTTP POST request in Node.js.

In this article, we’ll look at how to make a HTTP POST request in Node.js.

How to make a HTTP POST request in Node.js?

To make a HTTP POST request in Node.js, we can use the axios package.

We install it by running

npm i axios

Then we use it by writing

const axios = require('axios');

const makeRequest = async () => {
  const data = {
    name: 'John Doe',
    job: 'Content Writer'
  };

  const {
    data: resData,
    status
  } = await axios.post('https://reqres.in/api/users', data)
  console.log(resData, status)
}

to call axios.post to make the request.

The data object is the JSON payload.

post returns a promise that resolves to an object with the data and status properties.

data has the response data and status has the response status code.

Conclusion

To make a HTTP POST request in Node.js, we can use the axios package.

Categories
JavaScript Answers

How to parse JSON using Node.js?

Sometimes, we want to parse JSON using Node.js.

In this article, we’ll look at how to parse JSON using Node.js.

How to parse JSON using Node.js?

To parse JSON using Node.js, we can use require.

For instance, we write

const config = require('./config.json');

to call require with the JSON file’s path and assign the returned JSON object to config.

require loads the JSON synchronously like any other module and the file is only read once when we call require and then cached thereafter.

Conclusion

To parse JSON using Node.js, we can use require.