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.

Categories
JavaScript Answers

How to pass JSON to HTTP POST request made with Node.js request?

Sometimes, we want to pass JSON to HTTP POST request made with Node.js request.

In this article, we’ll look at how to pass JSON to HTTP POST request made with Node.js request.

How to pass JSON to HTTP POST request made with Node.js request?

To pass JSON to HTTP POST request made with Node.js request, we call request with the json option set to true and the body option set to a plain JavaScript object with the request body.

For instance, we write

const rp = require('request-promise');

await rp({
  method: 'POST',
  uri: 'http://localhost:3000/',
  body: {
    val1: 1,
    val2: 2
  },
  json: true
})

to call rp with the body set a request body object.

And we set json to true to send the body as a JSON payload.

We set method to 'POST' to make a POST request.

Conclusion

To pass JSON to HTTP POST request made with Node.js request, we call request with the json option set to true and the body option set to a plain JavaScript object with the request body.

Categories
JavaScript Answers

How to get raw request body using Express and Node.js?

Sometimes, we want to get raw request body using Express and Node.js.

In this article, we’ll look at how to get raw request body using Express and Node.js.

How to get raw request body using Express and Node.js?

To get raw request body using Express and Node.js, we can use the body-parser package.

For instance, we write

const bodyParser = require('body-parser');

app.use(bodyParser.raw(options));

app.get(path, (req, res) => {
  console.log(req.body)
});

to call bodyParser.raw to return a middleware that parses the request body into a buffer.

Then we call app.use with the returned middleware to use it our app.

Finally, in our GET request handler, we get the raw request body as a buffer object from req.body.

Conclusion

To get raw request body using Express and Node.js, we can use the body-parser package.