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.

Categories
JavaScript Answers

How to fix the “Cannot find module ‘@babel/core'” error?

Sometimes, we want to fix the "Cannot find module ‘@babel/core’" error.

In this article, we’ll look at how to fix the "Cannot find module ‘@babel/core’" error.

How to fix the "Cannot find module ‘@babel/core’" error?

To fix the "Cannot find module ‘@babel/core’" error, we should install the @babel/core package.

To install it, we run

npm install @babel/core --save

Conclusion

To fix the "Cannot find module ‘@babel/core’" error, we should install the @babel/core package.

Categories
JavaScript Answers

How to fix the ‘schema hasn’t been registered for model’ error with Mongoose?

Sometimes, we want to fix the ‘schema hasn’t been registered for model’ error with Mongoose

In this article, we’ll look at how to fix the ‘schema hasn’t been registered for model’ error with Mongoose.

How to fix the ‘schema hasn’t been registered for model’ error with Mongoose?

To fix the ‘schema hasn’t been registered for model’ error with Mongoose, we should call require on the model files we created.

For instance, we write

/models/Posts.js

const mongoose = require('mongoose');

const PostSchema = new mongoose.Schema({
  title: String,
  link: String,
  upvotes: {
    type: Number,
    default: 0
  },
  comments: [{
    type: mongoose.Schema.Types.ObjectId,
    ref: 'Comment'
  }]
});

mongoose.model('Post', PostSchema)

to create the PostSchema and register it as 'Post' with the mongoose.model method.

Then we register the models after we connect to the database with

db.js

const mongoose = require('mongoose');
mongoose.connect('mongodb://localhost/news');
require('./models/Posts');

to call require with './models/Posts' to run the code in /models/Posts.js which registers the model Post model so we can use it.

Conclusion

To fix the ‘schema hasn’t been registered for model’ error with Mongoose, we should call require on the model files we created.