title: How to send JSON from server-side (nodejs) to client-side with Ajax?
body: <p>I've created a Node.js/express server to GET call API. The API returns a json data. How do I send the JSON I've received to my local javascript (client-server)? I'd like to grab this JSON, send to my local client (with an ajax call if this is the option), loop through it and append it to my DOM!</p>
  
  <p>app.js</p>
  
  <pre><code>var express = require('express');
  var router = express.Router();
  var request = require("request");
  
  let name, value, bodyData;
  
  var options = { method: 'GET',
    url: 'https://api.example.com/data1',
    qs: 
     { 
       valueType: 'MAXIMUM'
     },
    headers: 
     { 
       authorization: 'ABC123456',
       accept: 'application/json; charset=utf-8' } };
  
  request(options, function (error, response, body) {
    if (error) throw new Error(error);
  
    bodyData = body;
  
  });
  
  
  router.get('/', function(req, res, next) {
    res.render('home', {data: bodyData});
  });
  
  module.exports = router;
  </code></pre>
  
  <p>app.ejs</p>
  
  <pre><code>&lt;!DOCTYPE html&gt;
  &lt;html lang="en"&gt;
  &lt;head&gt;
      &lt;meta charset="UTF-8"&gt;
      &lt;meta name="viewport" content="width=device-width, initial-scale=1.0"&gt;
      &lt;meta http-equiv="X-UA-Compatible" content="ie=edge"&gt;
      &lt;title&gt;Items&lt;/title&gt;
  &lt;/head&gt;
  &lt;body&gt;
    &lt;div id="itemsList"&gt;
      &lt;h2&gt;List of available items:  &lt;/h2&gt;
    &lt;/div&gt;
  &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>
  
  <p>scripts.js</p>
  
  <pre><code>$(document).ready(function(){
     $.ajax({
         type: "GET",
         url: "http://localhost:3000/",
         contentType: "application/json"
       }).done(function(data){
            for(var i =1; i&lt;= 10; i++){
            $('#itemsList').append('&lt;div id="r'+ i +'"&gt;&lt;/div&gt;')
            }
       })
   });
  </code></pre>