Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

160
Visualizações
How to do api calls without fetch in nextjs?

I have a NextJS project with express backend that requires some data fetching in getServerSideProps.

The problem is await fetch('/api/anyApi') does not work with relative path and calls to absolute path await fetch('${config.OPTION_HOST}/api/anyApi' are getting blocked by some kind of protection on host machine.

I wish to fix this problem instead and just use absolute paths but I've been told that's not possible right now so I need to rewrite getServerSideProps everywhere to not use fetch.

Best solution I could find without rewriting big chunk of backend logic was to use mocks like in this anwser.

So instead of const result = await fetch('/api/menu/${context.locale}'), I ended up with this code:

export async function getServerSideProps(context) {
  
  const categoryController = require('../../../server/controllers/category')
  
  var MockExpressRequest = require('mock-express-request');
  var MockExpressResponse = require('mock-express-response');

  var requestMenu = new MockExpressRequest( {
    params: {
      locale: context.locale,
    }
  })
  var responseMenu = new MockExpressResponse();

  await categoryController.getCategories(requestMenu, responseMenu);

  const result = responseMenu

...rest of getServerSideProps...
}

I'm new to programing and while this solution works, I have a feeling that using a library for testing in production code might be bad idea. What do you think?

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

You could use AXIOS instead: https://www.npmjs.com/package/axios

You can find all the AXIOS documentation here: https://axios-http.com/

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda