-1

Im sorry if this may be a dumb question but i hit a wall and been trying to figure this out for hours.

How to call json api and and add it to html page via node.js

my api address is localhost:8080/api I'm routing my html page to index.html. I'm guessing it to make son an array and then call array in html? I have n idea how to do that. please help me out. thanks.

server.js`

var util = require('./util.js');

//call random gen - return string with 9 length.
var output = util.stringGen(9);

console.log(output);


var express = require('express');
var app = express();
var path = require('path');

// viewed at http://localhost:8080
app.get('/', function(req, res) {
    res.sendFile(path.join(__dirname + '/index.html'));
});


app.get('/api', function(req, res){
    res.json({healthy: output})
});

app.listen(8080);

Froggy
  • 41
  • 5
  • What are you using to do the API call? – Baruch Jan 05 '17 at 14:35
  • Are you asking about making an api call in Javascript and displaying response on html page? – Rohan Kumar Jan 05 '17 at 14:37
  • this is my server.js var util = require('./util.js'); //call random gen - return string with 9 length. var output = util.stringGen(9); console.log(output); var express = require('express'); var app = express(); var path = require('path'); // viewed at http://localhost:8080 app.get('/', function(req, res) { res.sendFile(path.join(__dirname + '/index.html')); }); app.get('/api', function(req, res){ res.json({healthy: output}) }); app.listen(8080); – Froggy Jan 05 '17 at 14:41
  • ok i added my server.js – Froggy Jan 05 '17 at 14:42
  • @RohanKumar yes i am – Froggy Jan 05 '17 at 14:45
  • @Baruch i added my server.js – Froggy Jan 05 '17 at 14:49
  • @Froggy actually we need to see your client code, show us how you are using the API, your client-side javascript – AlexD Jan 05 '17 at 14:51
  • for hitting api you should use request module. But, what do you mean by call array in html? Or do you want to hit api on client side? – Rohan Kumar Jan 05 '17 at 14:53
  • Thats what I'm asking how to make the client of what I'm doing. – Froggy Jan 05 '17 at 14:54
  • hit api on clientside – Froggy Jan 05 '17 at 14:54
  • @RohanKumar I'm trying hit api on client side. – Froggy Jan 05 '17 at 14:56
  • Possible duplicate of [How do I call a web service from javascript](http://stackoverflow.com/questions/118565/how-do-i-call-a-web-service-from-javascript) – Heretic Monkey Jan 05 '17 at 15:06

2 Answers2

0

Your question is how to call your server from your client.

You might choose different approaches depending on what client you are using.

For native javascript, you can try reading here or if you are using AngularJS, see how to use $http

The URL you would want to call is `http://localhost:8080/api'

AlexD
  • 4,062
  • 5
  • 38
  • 65
0

To hit api on client side, you need to use XMLHttpRequest . I've added a simple example of how to do that using github api and displaying the response in the label field. Although you can display response in any way you want :

<body>
<script>

function httpGet(theURL) {
  var xmlHttp = new XMLHttpRequest();
  xmlHttp.open("GET", theURL);
  xmlHttp.setRequestHeader("Content-Type", "application/json");
  xmlHttp.send(null);


  xmlHttp.onreadystatechange = function() {
    var response = null;
    if(xmlHttp.readyState == XMLHttpRequest.DONE) {
      response = xmlHttp.responseText;
      document.getElementById("response").value = response;
    }
  };
}

</script>

<input id="clickMe" type="button" value="get Data" 
  onclick="httpGet('https://api.github.com/search/users?q=rohankanojia')" />

<br>
Response : <input id="response" type="label"/>
</body>
Rohan Kumar
  • 5,427
  • 8
  • 25
  • 40