Categories
JavaScript Answers

How to fix ‘Error: unable to verify the first certificate’ in Node.js?

Sometimes, we want to fix ‘Error: unable to verify the first certificate’ in Node.js.

In this article, we’ll look at how to fix ‘Error: unable to verify the first certificate’ in Node.js.

How to fix ‘Error: unable to verify the first certificate’ in Node.js?

To fix ‘Error: unable to verify the first certificate’ in Node.js, we should set the appropriate root certificate.

For instance, we write

require('https').globalAgent.options.ca = require('ssl-root-cas/latest').create();

to call require with 'ssl-root-cas/latest' to require the root certificate.

Then we can set that has the value of the require('https').globalAgent.options.ca property so https would use the required root certificate.

Conclusion

To fix ‘Error: unable to verify the first certificate’ in Node.js, we should set the appropriate root certificate.

Categories
JavaScript Answers

How to add automatic HTTPS connection or redirect with Node.js and Express?

Sometimes, we want to add automatic HTTPS connection or redirect with Node.js and Express.

In this article, we’ll look at how to add automatic HTTPS connection or redirect with Node.js and Express.

How to add automatic HTTPS connection or redirect with Node.js and Express?

To add automatic HTTPS connection or redirect with Node.js and Express, we can call res.redirect.

For instance, we write

const http = express();

http.get('*', (req, res) => {
  res.redirect('https://' + req.headers.host + req.url);

})

http.listen(8080);

to call res.redirect with the URL we want to redirect to.

We get the hostname with req.headers.host.

And we get the rest of the URL from req.url.

Conclusion

To add automatic HTTPS connection or redirect with Node.js and Express, we can call res.redirect.

Categories
JavaScript Answers

How to set the default time zone in Node.js?

Sometimes, we want to set the default time zone in Node.js.

In this article, we’ll look at how to set the default time zone in Node.js.

How to set the default time zone in Node.js?

To set the default time zone in Node.js, we can set the TZ environment variable.

For instance, we write

process.env.TZ = 'Europe/Amsterdam'

to set TZ to the 'Europe/Amsterdam' time zone.

Now all local times are returned in the Europe/Amsterdam time zone.

Conclusion

To set the default time zone in Node.js, we can set the TZ environment variable.

Categories
JavaScript Answers

How to get the response body with Node.js http.get?

Sometimes, we want to get the response body with Node.js http.get.

In this article, we’ll look at how to get the response body with Node.js http.get.

How to get the response body with Node.js http.get?

To get the response body with Node.js http.get, we can get it from the callback we pass into http.request.

For instance, we write

const options = {
  host: 'www.example.com',
  port: 80,
  path: '/upload',
  method: 'POST'
};

const req = http.request(options, (res) => {
  console.log('STATUS: ' + res.statusCode);
  console.log('HEADERS: ' + JSON.stringify(res.headers));
  res.setEncoding('utf8');
  res.on('data', (chunk) => {
    console.log('body: ' + chunk);
  });
});

req.on('error', (e) => {
  console.log('problem with request: ' + e.message);
});

req.write('data\n');
req.write('data\n');
req.end();

to call http.request with the options object and a callback that has the res parameter.

In it, we call res.on with 'data' to listen to the data event which has the response data set as the value of chunk.

The data event is emitted when response data is received.

The response headers are in res.headers.

The response status code is in res.statusCode.

When there’s an error with the request, the 'error' event handler runs.

We listen for them with

req.on('error', (e) => {
  console.log('problem with request: ' + e.message);
});

And we call req.write to write data to the request body.

Finally, we close the handle with req.end.

Conclusion

To get the response body with Node.js http.get, we can get it from the callback we pass into http.request.

Categories
JavaScript Answers

How to make a HTTP GET request in Node.js Express?

Sometimes, we want to make a HTTP GET request in Node.js Express.

In this article, we’ll look at how to make a HTTP GET request in Node.js Express.

How to make a HTTP GET request in Node.js Express?

To make a HTTP GET request in Node.js Express, we can use the axios.get method.

For instance, we write

const axios = require('axios');

const f = async () => {
  const {
    data
  } = await axios.get('https://example.com')
}

to call axios.get with the URL we want to make the GET request to.

axios.get returns a promise that resolves to the response.

Then we get the response data from the data property returned by the promise.

Conclusion

To make a HTTP GET request in Node.js Express, we can use the axios.get method.