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

233
Views
How to return data using a server side API call?

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)
        })

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

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)
}
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!