Categories
JavaScript Answers

How to run Node.js http-server with SSL?

Sometimes, we want to run Node.js http-server with SSL.

In this article, we’ll look at how to run Node.js http-server with SSL.

How to run Node.js http-server with SSL?

To run Node.js http-server with SSL, we should run https-server with the certificate file path as one of the option values.

For instance, we create a certificate with

mkcert 0.0.0.0 localhost 127.0.0.1 ::1

to create a certificate with mkcert after changing into the project directory.

Then we rename 0.0.0.0+3-key.pem to key.pem and 0.0.0.0+3.pem to cert.pem.

Finally, we run the https-server with cert.pem with

http-server -S -C cert.pem -o

Conclusion

To run Node.js http-server with SSL, we should run https-server with the certificate file path as one of the option values.

Categories
JavaScript Answers

How to call a JSON API with Node.js?

Sometimes, we want to call a JSON API with Node.js.

In this article, we’ll look at how to call a JSON API with Node.js.

How to call a JSON API with Node.js?

To call a JSON API with Node.js, we can use the http module.

For instance, we write

const url = 'http://graph.facebook.com/517267866/?fields=picture';

http.get(url, (res) => {
  let body = '';

  res.on('data', (chunk) => {
    body += chunk;
  });

  res.on('end', () => {
    const fbResponse = JSON.parse(body);
    console.log("Got a response: ", fbResponse.picture);
  });
}).on('error', (e) => {
  console.log("Got an error: ", e);
});

to call http.get with a url to make a request to the URL.

Then we get the response from res.

And then we call res.on with 'data' to listen to the data event that we get from the chunk.

We concatenate the chunks to body to get the whole response body.

The whole response body should be available in the 'end' event callback since we concatenated the chunks to body.

Conclusion

To call a JSON API with Node.js, we can use the http module.

Categories
JavaScript Answers

How to fix ‘CastError: Cast to ObjectId failed for value “[object Object]” at path “_id”‘ error with Mongoose?

Sometimes, we want to fix ‘CastError: Cast to ObjectId failed for value "[object Object]" at path "_id"’ error with Mongoose.

In this article, we’ll look at how to fix ‘CastError: Cast to ObjectId failed for value "[object Object]" at path "_id"’ error with Mongoose.

How to fix ‘CastError: Cast to ObjectId failed for value "[object Object]" at path "_id"’ error with Mongoose?

To fix ‘CastError: Cast to ObjectId failed for value "[object Object]" at path "_id"’ error with Mongoose, we should check the object IDs we’re querying with are valid object IDs.

For instance, we write

const mongoose = require('mongoose');

console.log(mongoose.Types.ObjectId.isValid('53cb6b9b4f4ddef1ad47f943'));

to call mongoose.Types.ObjectId.isValid with a string to check if the string is a valid object ID.

Conclusion

To fix ‘CastError: Cast to ObjectId failed for value "[object Object]" at path "_id"’ error with Mongoose, we should check the object IDs we’re querying with are valid object IDs.

Categories
JavaScript Answers

How to make a HTTP get request with query string parameters with Node.js?

Sometimes, we want to make a HTTP get request with query string parameters with Node.js.

In this article, we’ll look at how to make a HTTP get request with query string parameters with Node.js.

How to make a HTTP get request with query string parameters with Node.js?

To make a HTTP get request with query string parameters with Node.js, we can use the https.get method.

For instance, we write

const requestUrl = url.parse(url.format({
  protocol: 'https',
  hostname: 'example.com',
  pathname: '/the/path',
  query: {
    key: value
  }
}));

const req = https.get({
  hostname: requestUrl.hostname,
  path: requestUrl.path,
}, (res) => {
  // ...
})

to call url.parse to create a URL with the protocol, hostname, pathname and the query string parameters in the query property’s object.

Then we call https.get with an object with the hostname and path set to the hostname and path.

The path has the query string.

And we get the response from res in the callback.

Conclusion

To make a HTTP get request with query string parameters with Node.js, we can use the https.get method.

Categories
JavaScript Answers

How to convert Mongoose docs to JSON?

Sometimes, we want to convert Mongoose docs to JSON

In this article, we’ll look at how to convert Mongoose docs to JSON.

How to convert Mongoose docs to JSON?

To convert Mongoose docs to JSON, we can use the lean method.

For instance, we write

UserModel.find().lean().exec((err, users) => {
  console.log(users);
})

to call lean after find to convert the query results returned by find to a plain JavaScript object.

Then we call exec with a callback to get the returned query results from users.

Conclusion

To convert Mongoose docs to JSON, we can use the lean method.