Skip to content

[Local][Node.js] ECONNREFUSED ::1:1337 on local development with Axios and Nodejs app #13680

@JulienHe

Description

@JulienHe

Bug report

Required System information

  • Node.js version: v16.15.1
  • NPM version: 8.11.0
  • Strapi version: v4.1.12
  • Database: sqlite
  • Operating system: Mac OS Monterey 12.1

Describe the bug

While I'm working on a Google Cloud Functions to manage the publication status of my content, I wanted to make it more automatic. I wrote a function that let me get all the content that have a publicationState: PREVIEW with a specific date, in my case today. I work with GraphQL on this project and I tried my queries on Postman to get a grasp of it.
What I tried to achieve is to update my publications to be updated from PREVIEW to LIVE without any human interaction. I avoid any plugin to do so as I can't run any cron on my app engine in production.

  • When I run the query on Postman, I get the content I need, I have no problem.
  • When I run the CURL snipper from postman on my terminal, I have no problem.
  • When I run the Node.js code on my production environment, I have no problem
  • When I run the Node.js code on my local environment, I get an error.

Expected behavior

Be able to query http://localhost:1337/graphql on my node.js app locally.

Screenshots

Postman

CleanShot 2022-07-04 at 08 40 50@2x

Curl

curl --location --request POST 'http://localhost:1337/graphql' \
--header 'Content-Type: application/json' \
--data-raw '{"query":"query getSnacks($date: Date) {\n    snacks(publicationState: PREVIEW, filters: { publish_at: { eq: $date } }) {\n      data {\n        id\n      }\n    }\n  }","variables":{"date":"2022-07-03"}}'

CleanShot 2022-07-04 at 08 42 43@2x

Node.js

CleanShot 2022-07-04 at 08 45 09@2x

https://jsonhero.io/j/5C1RgIvUu6TS

Code snippets

Function

'use strict';
const functions = require('@google-cloud/functions-framework');
const axios = require('axios');
require('dotenv').config()

functions.http('updatePublication', (req, res) => {
    const today = new Date();
    const dd = String(today.getDate()).padStart(2, "0");
    const mm = String(today.getMonth() + 1).padStart(2, "0"); //January is 0!
    const yyyy = today.getFullYear();
    
    const getPublication = JSON.stringify({
        query: `query getSnacks($date: Date) {
            snacks(publicationState: PREVIEW, filters: { publish_at: { eq: $date } }) {
                data {
                    id
                }
            }
        }`,
        variables: {"date":`${yyyy}-${mm}-${dd}`}
    });
    const configGetPublication = {
        method: 'post',
        url: `${process.env.GRAPHQL_API_URL}`,
        headers: { 
            'Content-Type': 'application/json'
        },
        data : getPublication
    };
    
    const sendGetRequest = async () => {
        try {
            const resp = await axios(configGetPublication);
            console.log(JSON.stringify(resp.data));
            res.send(JSON.stringify(resp.data));
        } catch (err) {
            console.error(JSON.stringify(err));
            res.send(JSON.stringify(err));
        }
    };
    
    sendGetRequest();
});

Middleware

module.exports = [
  "strapi::errors",
  {
    name: "strapi::security",
    config: {
      contentSecurityPolicy: {
        useDefaults: true,
        directives: {
          "connect-src": ["'self'", "https:"],
          "img-src": ["'self'", "data:", "blob:"],
          "media-src": [
            "'self'",
            "data:",
            "blob:",
          ],
          upgradeInsecureRequests: null,
        },
      },
    },
  },
  "strapi::cors",
  "strapi::poweredBy",
  "strapi::logger",
  "strapi::query",
  "strapi::body",
  "strapi::session",
  "strapi::favicon",
  "strapi::public",
];

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions