Https. Request() Is Giving Error: Connect Econrefused 127.0.0.1:443

Hello there, I'm at my wits end I'm struggling I would like to know where I'm going wrong I can't seem to get past this error connect ECONNREFUSED 127.0.0.1:443 something tells me it's something small that I'm neglecting but I can't see it. I read here that it's possible that the is a network overload problem but I have shut everything down except my ngrok to simulate https and the mongodb running yet still no joy. Any and all suggestions appreciated

const https = require('https');

app.get('/auth/instagram', (req, res) => {
  const clientId = process.env.INSTAGRAM_ID;
  const redirectUri = process.env.INSTAGRAM_ID_URI;
  res.redirect(`);
});
app.get('/auth/instagram/callback', (req, res) => {
  // console.log('this is the code', req.query.code);

  const { code } = req.query;
  console.log('this is the code ', code);
  const redirectURL = `${process.env.BASE_URL}/auth/instagram/callback`;
  const url = '
  const options = {
    method: 'post',
    headers: {
      'Content-Type': 'application/x-www-form-urlencoded'
    },
    body: {
      client_id: process.env.INSTAGRAM_ID,
      client_secret: process.env.INSTAGRAM_SECRET,
      grant_type: 'authorization_code',
      redirect_uri: redirectURL,
      code
    },
    json: true,
    url
  };
  console.log('This is that options', options);
  https.request(options, (err, res) => {
    console.log('this is the body', res);
  });
  process.on('uncaughtException', (err) => {
    console.log(err);
  });

});

Error

Error: connect ECONNREFUSED 127.0.0.1:443
    at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1141:16) {
  errno: 'ECONNREFUSED',
  code: 'ECONNREFUSED',
  syscall: 'connect',
  address: '127.0.0.1',
  port: 443
}

2 Answers

You can not use PORT 443 for your Node.js Application, because of this port used by the HTTP server for Secure HTTP or https:// connection. Use a different port address. If you want to use PORT 443 then install the Nginx server and connect your Node.js application in Reverse Proxy methodology. You need to install an SSL Certificate and bind it into the Nginx Config file if you want to use https:// for your Node.js Application.

You can also follow this tutorial. Click this link

2

Try setting your options with hostname and path parameters, and remove 'url'. Other libraries often use 'url' : url so that is likely the problem. Node's https uses host or hostname and path. See this.

  const options = {
    method: 'post',
    hostname: 'api.instagram.com',
    path: '/oauth/access_token',    
    headers: {
      'Content-Type': 'application/x-www-form-urlencoded'
    },
    body: {
      client_id: process.env.INSTAGRAM_ID,
      client_secret: process.env.INSTAGRAM_SECRET,
      grant_type: 'authorization_code',
      redirect_uri: redirectURL,
      code
    },
    json: true
  };      

If you want to use a url, it should be declared as a URL Object, for example:

const options = new URL('[email protected]');

Or you can use a url string. In the case of a string or a URL Object, you cannot have additional options, so it is not all that useful for your situation.

I'll add that I had this exact same error with a Node GCP Cloud Function, and this fixed it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Sarah Jenkins

Sarah Jenkins

Senior Technology Editor & AI Specialist

Sarah Jenkins is a veteran tech journalist with over 12 years of experience covering artificial intelligence, mobile innovations, and digital ethics. Her insights have appeared in leading technology publications worldwide.

Share this article
Twitter Facebook Pinterest