Nodejs Fetch API: unable to verify the first certificate
Asked Answered
A

2

5

I'm using the fetch API module in my Philips Hue project and when I make a call to the local ip address (my hub) it produces that error in the title.

const fetch = require('node-fetch');
const gateway = "192.168.0.12";
const username = "username";

let getLights = function(){
    fetch(`https://${gateway}/api/${username}/lights`, {
        method: 'GET'
    }).then((res) => {
        return res.json();
    }).then((json) => {
        console.log(json);
    });
}


module.exports = {getLights};

Any SECURE fix this will eventually go onto the public internet for me to access my lights from anywhere sooo?

Aaron answered 5/7, 2020 at 20:57 Comment(0)
R
1

It seems like you tried to access it using HTTPS. Most likely on your local network it is going to be HTTP

So by changing https://${gateway}/api/${username}/lights to http://${gateway}/api/${username}/lights should work.

If you're trying to keep it HTTPS then you will have to install a SSL certificate authority onto your network.
These may be useful sources if you're trying to get that done:

https://www.freecodecamp.org/news/how-to-get-https-working-on-your-local-development-environment-in-5-minutes-7af615770eec/

https://letsencrypt.org/docs/certificates-for-localhost/

Religion answered 5/7, 2020 at 21:51 Comment(0)
G
11

To skip the SSL tests, you can use this:

process.env['NODE_TLS_REJECT_UNAUTHORIZED'] = 0;

Goldeye answered 9/8, 2021 at 9:24 Comment(2)
is it possible to add the ssl cert anywhere instead of making everything insecure ?Sula
This makes the connection insecure, and is dangerous for anything but local devPhaidra
R
1

It seems like you tried to access it using HTTPS. Most likely on your local network it is going to be HTTP

So by changing https://${gateway}/api/${username}/lights to http://${gateway}/api/${username}/lights should work.

If you're trying to keep it HTTPS then you will have to install a SSL certificate authority onto your network.
These may be useful sources if you're trying to get that done:

https://www.freecodecamp.org/news/how-to-get-https-working-on-your-local-development-environment-in-5-minutes-7af615770eec/

https://letsencrypt.org/docs/certificates-for-localhost/

Religion answered 5/7, 2020 at 21:51 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.