Categories
JavaScript Answers

How to fix the ‘BodyParser is deprecated’ warning with Node.js and Express?

Sometimes, we want to fix the ‘BodyParser is deprecated’ warning with Node.js and Express.

In this article, we’ll look at how to fix the ‘BodyParser is deprecated’ warning with Node.js and Express.

How to fix the ‘BodyParser is deprecated’ warning with Node.js and Express?

To fix the ‘BodyParser is deprecated’ warning with Node.js and Express, we can replace bodyParser with express.urlencoded and express.json.

For instance, we write

app.use(express.urlencoded({
  extended: true
}));
app.use(express.json())

to call app.use with the middlewares returned by express.urlencoded and express.json to let us parse URL encoded and JSON request bodies.

Conclusion

To fix the ‘BodyParser is deprecated’ warning with Node.js and Express, we can replace bodyParser with express.urlencoded and express.json.

Categories
JavaScript Answers

How to fix the hostname or IP not matching certificate’s altnames with Node.js?

Sometimes, we want to fix the hostname or IP not matching certificate’s altnames with Node.js.

In this article, we’ll look at how to fix the hostname or IP not matching certificate’s altnames with Node.js.

How to fix the hostname or IP not matching certificate’s altnames with Node.js?

To fix the hostname or IP not matching certificate’s altnames with Node.js, we can use http-proxy with changeOrigin to true.

For instance, we write

const proxy = httpProxy.createProxyServer();

proxy.web(req, res, {
  changeOrigin: true,
  target: 'https://example.com:3000'
});

to create a proxy to proxy the traffic to example.com:3000.

Then we create the server with

httpProxy.createServer({
  ssl: {
    key: fs.readFileSync('valid-ssl-key.pem', 'utf8'),
    cert: fs.readFileSync('valid-ssl-cert.pem', 'utf8')
  },
  target: 'https://example.com:3000',
  secure: true
}).listen(443);

to read the key and certificate with fs.readFileSync and then we set target to the hostname we’re hosting our server from.

Conclusion

To fix the hostname or IP not matching certificate’s altnames with Node.js, we can use http-proxy with changeOrigin to true.

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.