Categories
JavaScript Answers

How to read file as a base64 string with Node.js?

Sometimes, we want to read file as a base64 string with Node.js

In this article, we’ll look at how to read file as a base64 string with Node.js.

How to read file as a base64 string with Node.js?

To read file as a base64 string with Node.js, we can use the readFile method with the encoding option.

For instance, we write

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

const read = async () => {
  const contents = await fs.readFile('/path/to/file.jpg', {
    encoding: 'base64'
  });
}

to call fs.readFile with the file path and an object with the encoding property set to 'base64' to read the file as a base64 string.

It returns a promise, so we use await to get the read file contents from the resolved value.

We require the promise version of the fs module with

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

Conclusion

To read file as a base64 string with Node.js, we can use the readFile method with the encoding option.

Categories
JavaScript Answers

How to replace callbacks with promises in Node.js?

Sometimes, we want to replace callbacks with promises in Node.js.

In this article, we’ll look at how to replace callbacks with promises in Node.js.

How to replace callbacks with promises in Node.js?

To replace callbacks with promises in Node.js, we can use the util.promisify method.

For instance, we write

import mysql from 'mysql';
const util = require('util');

const connection = mysql.createConnection({
  host: 'localhost',
  user: 'user',
  password: 'password',
  database: 'db'
});

const connectAsync = util.promisify(connection.connectAsync);
const queryAsync = util.promisify(connection.queryAsync);

const getUsersAsync = async () => {
  await connectAsync()
  return queryAsync('SELECT * FROM Users')
};

to convert the connection.connectAsync and connection.queryAstnc methods to functions that returns promises with util.promisify.

Then in the getUsersAsync function, we call them both.

We use await to wait for the promise returned by connectAsync to resolve before calling queryAsync.

Conclusion

To replace callbacks with promises in Node.js, we can use the util.promisify method.

Categories
JavaScript Answers

How to get data out of a Node.js HTTP get request?

Sometimes, we want to get data out of a Node.js HTTP get request.

In this article, we’ll look at how to get data out of a Node.js HTTP get request.

How to get data out of a Node.js HTTP get request?

To get data out of a Node.js HTTP get request, we can get the data from the data event callback.

For instance, we write

const http = require('http')

http.get(options, (response) => {
  response.setEncoding('utf8')
  response.on('data', console.log)
  response.on('error', console.error)
})

to call http.get to make a HTTP GET request.

We get the request response from the 'data' event payload.

To get the data from the payload, we call response.on with 'data' and a callback that has the response data in the first parameter.

Therefore, when the 'data' event callback is run, we see the response data logged with console.log.

Conclusion

To get data out of a Node.js HTTP get request, we can get the data from the data event callback.

Categories
JavaScript Answers

How to use SHA-256 with Node.js crypto?

Sometimes, we want to use SHA-256 with Node.js crypto.

In this article, we’ll look at how to use SHA-256 with Node.js crypto.

How to use SHA-256 with Node.js crypto?

To use SHA-256 with Node.js crypto, we call the createHash method.

For instance, we write

const hash = crypto.createHash('sha256').update(pwd).digest('base64');

to call createHash with 'sha256' and call update with the string we want to creatre the has from to create the hash.

Then we return the hash digest string from the hash with the digest method.

We pass in 'base64' as the argument, so the base64 hash digest is returned.

Also, we can pass in 'hex' to digest to return a hex hash digest.

Conclusion

To use SHA-256 with Node.js crypto, we call the createHash method.

Categories
JavaScript Answers

How to read a file from AWS S3 bucket using Node.js fs module?

Sometimes, we want to read a file from AWS S3 bucket using Node.js fs module.

In this article, we’ll look at how to read a file from AWS S3 bucket using Node.js fs module.

How to read a file from AWS S3 bucket using Node.js fs module?

To read a file from AWS S3 bucket using Node.js fs module, we can use the s3.getObject method.

For instance, we write

s3.getObject({
  Bucket,
  Key: keyName
}, (err, data) => {
  if (err) {
    return
  }
  console.log(data);
});

to call getObject with params which has the bucket and key.

Then we get the read file from the data parameter in the callback.

Conclusion

To read a file from AWS S3 bucket using Node.js fs module, we can use the s3.getObject method.