What is the best way to send my JSON Data as a variable to a client side script

Making a stock ticker. Kind of lost. Making an api request from a node server. How do I share my variable with other .js files?

Thanks for the help!

13 thoughts on “What is the best way to send my JSON Data as a variable to a client side script”

  1. You can use res.json to send the data and you can use something axios to parse the response and store it in a variable

    also using cors to protect the api (Since you metioned private api)

    Server side

    const https = require('https')
    var cors = require('cors')
    const express = require('express')
    const app = express()
    var corsOptions = {
      origin: 'http://example.com',
      optionsSuccessStatus: 200 
    }
    app.get('/',cors(corsOptions) , function (req, res) {
    
    const options = {
      hostname: 'whatever.com',
      port: 443,
      path: '/todos',
      method: 'GET'
    }
    let data
    
    const req = https.request(options, result => {
      data = JSON.parse(result.data)
    })
      return res.json(data)
    })
    app.listen(3000)
    

    Client Side

    const axios = require('axios');
    axios.get('<api url>').then(function (response) {
        console.log(response.data);//contains the header
      })
    
    
    Reply

Leave a Comment