Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

161
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!