Home > OS >  How to return data using a server side API call?
How to return data using a server side API call?

Time:11-16

I'm trying to invoke my api URL in my back end node.js application. Previously I was passing the entire invoke url on my front end ajax call, but I wanted to switch it to a server side API call so that I dont expose the invoke url. I'm not to familiar with calling an external API on the backend, so I was hoping to get some advice. I'm trying to use the MVC pattern to keep the code clean.

This is the contoller

require('dotenv').config();
module.exports = {
    getModel: (req, res) => {
        const { buildingNumber, commodity, meter, trainStart, trainEnd, analysisStart, analysisEnd } = req.query
        try {

            const model = process.env.API_URL   `building_number=${buildingNumber}&commodity_tag=${commodity}&meter=${meter}&train_start=${trainStart}&train_end=${trainEnd}&analysis_start=${analysisStart}&analysis_end=${analysisEnd}`
            return res.json(model)

        } catch (error) {
            console.error(error.message)
        }
    }
}

This is the route I'm using in the front end ajax call.

const router = require('express').Router()
const gatewayController = require('../controllers/apiGatewayModel')

router.get('/gateway', gatewayController.getModel);

module.exports = router

This is the front end ajax call used to invoke the api. When I run this ajax call on the front end, the response that gets logged to the console is the api url. This is an example of what is logged to the console after I try to run the ajax call.API_URL?building_number=0227&commodity_tag=S&meter=2032&train_start=2020-10-01&train_end=2021-09-29&analysis_start=2021-10-01&analysis_end=2021-10-31 I know the reason I'm only getting the api url returned in the console is because I have the url set to the variable model in the controller and then I'm just returning that model variable. How do I set up the controller to return the actual data from the API? Any advice is greatly appreciated!

    const modelApi = function () {
        const modelStart = $('.modelStart').val()
        const modelEnd = $('.modelEnd').val()
        const analysisStart = $('.analysisStart').val()
        const analysisEnd = $('.analysisEnd').val()

        $.ajax({
            url: '/gateway',
            method: 'GET',
            data: {
                buildingNumber: data[0][0].building_number,
                commodity: data[0][0].commodity_tag,
                meter: data[0][0].meter,
                trainStart: modelStart,
                trainEnd: modelEnd,
                analysisStart: analysisStart,
                analysisEnd: analysisEnd
            }
        }).then(response => {
            console.log(response)
        })

CodePudding user response:

Just use Axios to do the API call

require('dotenv').config();
const axios = require('axios').default;
module.exports = {
    getModel: async (req, res) => {
        const { buildingNumber, commodity, meter, trainStart, trainEnd, analysisStart, analysisEnd } = req.query
        try {

            const model = `${process.env.API_URL}building_number=${buildingNumber}&commodity_tag=${commodity}&meter=${meter}&train_start=${trainStart}&train_end=${trainEnd}&analysis_start=${analysisStart}&analysis_end=${analysisEnd}`
            const response = await axios.get(model)
            return res.json(response.data)

        } catch (error) {
            console.error(error.message)
        }
    }
}

Fetch call without jQuery :)

const modelApi =  async () => {
  const modelStart = $('.modelStart').val()
  const modelEnd = $('.modelEnd').val()
  const analysisStart = $('.analysisStart').val()
  const analysisEnd = $('.analysisEnd').val()
  const res = await fetch('/gateway', 
  {
    qs: {
    buildingNumber: buildingNumber, 
    commodity: commodity, 
    meter: meter, 
    trainStart: trainStart, 
    trainEnd: trainEnd, 
    analysisStart: analysisStart, 
    analysisEnd: analysisEnd}
  }).json();
  console.log(res)
}
  • Related