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.

Categories
JavaScript Answers

How to generate an MD5 hash in JavaScript and Node.js?

Sometimes, we want to generate an MD5 file hash in JavaScript and Node.js.

In this article, we’ll look at how to generate an MD5 file hash in JavaScript and Node.js.

How to generate an MD5 file hash in JavaScript and Node.js?

To generate an MD5 file hash in JavaScript and Node.js, we can use the crypto-js package.

To install it, we run

npm install crypto-js

Then we can create a MD5 hash string with

const MD5 = require("crypto-js/md5");

console.log(MD5("Message").toString());

We call MD5 with the string we want to create a hash from.

Then we call toString to return the hash string.

Conclusion

To generate an MD5 file hash in JavaScript and Node.js, we can use the crypto-js package.