Worker threads in Node js

207 views Asked by At

Unable to do axios calls inside worker threads

Hi, I have a node js application and I am trying to use worker threads for one of my use case.Inside my worker thread I need to do axios call.I was not able to do that and thread is giving me error with empty error object. Can anyone help me figure out the issue here

1

There are 1 answers

0
boojum On

Could you share a sample code? Here's a simple working code:

// index.js
const { Worker } = require("worker_threads");

const worker = new Worker("./worker.js");
worker.postMessage("run!");
// worker.js
const { parentPort } = require("worker_threads");
const Axios = require("axios");

const getRequest = async function () {
  const tiresias = await Axios({
    baseURL: "https://rickandmortyapi.com/api",
    url: "/character/1",
    method: "get",
    params: { s: "tets", min: 0.1 }
  });

  console.log(tiresias.data);
};
parentPort.once("message", (message) => getRequest());