narliecholler
narliecholler

Reputation: 479

Making an axios call inside my express controller, how do I return the status?

I'm not sure I am setting up my express controller to properly return the correct response. When the endpoint is hit, I want to call the address service with axios and return the data, or return the response if error. However, currently it returns the default error of 400 if cannot be found, but the response status will still be 200.

Is there a default methodology here that I am missing, or is this part way correct?

CONTROLLER

const getAddressWithPostcode = async (params: Params) => {
  const { postCode, number } = params

  const addressUrl = `${URL}/${postCode}${number
    ? `/${number}?api-key=${API_KEY}`
    : `?api-key=${API_KEY}`}`

  try {
    const { data } = await axios.get(addressUrl)
    return data
  } catch (e) {
    // throw e
    const { response: { status, statusText } } = e
    return {
      service: 'Address service error',
      status,
      statusText,
    }
  }
}

const findAddress = async (req: Request<Params>, res: Response, next: NextFunction) => {
  const { params } = req

  await getAddressWithPostcode(params)
    .then((data) => {
      res.send(data).status(200)
    })
    .catch((e) => {
      console.log('e', e)
      next(e)
    })
}

If I send a dodgy request (using postman) i get the response status 200 but the data returned is the object with the status and text. I want to make this my default response rather than returning an object with those properties. (see image below).

enter image description here

Just some direction here would be good, potentially of best practise using async await in express and error returning when using an external axios call within.

... ...

UPDATE:

updated my code to this, in response to an answer, I refactored my code a little.

const getAddressWithPostcode = async (params: Params) => {
  const { postCode, number } = params

  const addressUrl = `${URL}/${postCode}${number
    ? `/${number}?api-keey=${API_KEY}`
    : `?api-key=${API_KEY}`}`

  try {
    const { data } = await axios.get(addressUrl)
    return data
  } catch (e) {
    // throw e
    const { response } = e
    return response
  }

}

const findAddress = async (req: Request<Params>, res: Response, next: NextFunction) => {
  const { params } = req

  await getAddressWithPostcode(params)
    .then((data) => {
      console.log('data', data)
      if (data.status !== 200) res.sendStatus(data.status)
      else {
        res.send(data)
      }
    })
    .catch(err => {
      console.log('err', err)
      next(err)
    })
}

Upvotes: 2

Views: 1126

Answers (1)

MohammadTausif Shaikh
MohammadTausif Shaikh

Reputation: 543

If you want to send same http response code as you are getting from axios call then, just change below one line code in your controller.

// Every time send same http status code 200
res.send(data).status(200)

// Send same http status code as returned by axios request
res.send(data).status(data.status)

Upvotes: 2

Related Questions