0

This should be very simple, but it's turning out to be frustratingly tricky. I have an external server running that exposes an API endpoint. I need to call that API endpoint from a serverMiddleware (that is using express, btw) of a Nuxt2 app. I cannot simply call the API from the client-side, because I need the client request to first get authenticated and then it can send a call to the external API.

The project has gotten quite large at this point. So I'm putting a short snippet below to demonstrate what I'm trying to do:

const { Router } = require('express')
const router = Router()

const { authenticationMiddleware } = require('../helpers/authenticatedMiddleware')

router.post('/run', authenticationMiddleware, async function (req, res, next) {
  try {
    const { formInputs } = req.body

    const jsonResponse = await sendRequestToTheExternalAPI('https://external-api.com', formInputs) // Need help with it
    
    return res.json(jsonResponse)
  } catch (err) {
    next(err)
  }
})

module.exports = router

I've tried looking for many different approaches for the sendRequestToTheExternalAPI function:

  1. I was really hoping the axios module would work. But there seems to be a conflict between the @nuxtjs/axios and axios (which I'm facing too) when used alongside nuxt-auth. I'm unfortunately already using @nuxtjs/axios in the client-side and nuxt-auth for user authentication.
  2. Since I can't use both packages at the same time, I tried using @nuxtjs/axios for the server-side too. But simply importing it in place of axios doesn't work as I had hoped.
  3. There doesn't seem to be a builtin fetch function either, as I'm used to finding one in a node.js app.

Any help is greatly appreciated. Many thanks in advance.

0 Answers0