0

I created a js file that allow me to retrieve/output the data from an api, how would i display this file to a html page.

was using node.js to compile my data through the terminal

If you guys know a bunch a method , please link me it. thank you.

var realmStatus = "http://api.dol.gov/V1/Statistics/OES/OE_OCCUPATION/?KEY=1ce7650d-b131-4fb7-91b3-b7761efc8cd4";

var http = require("http");
var options = {
        host: 'api.dol.gov',
        path: realmStatus,
        type: 'GET',
        dataType: 'json',
        accept: 'application/json'
        };


 console.log("Start");
var x = http.request(options,function(res){
    console.log("Connected");
    res.on('data',function(data){

        data =JSON.stringify(data.toString());
        data =JSON.parse(data.toString());

       console.log(data.toString()+"\n");
    });
}});
x.end();
Ronnincd
  • 1
  • 3
  • 1
    Possible duplicate of [Display JSON as HTML](http://stackoverflow.com/questions/883977/display-json-as-html) – Matt Apr 07 '17 at 02:01
  • The link i seen , looks like that the data is save. my data im not saving it, im not storing that data onto a file but im getting it from a server though the get request. so that data is constantly updating from that server. – Ronnincd Apr 07 '17 at 02:08
  • are you making the api call in node.js or is it just a regular js file? – Daniel Persaud Apr 07 '17 at 02:22
  • its a regular js file i use from bracket, and i compile it through the terminal , to compile i have to type node file.js – Ronnincd Apr 07 '17 at 12:02

3 Answers3

0

if you're using Node try installing the Pug (formerly Jade) package. Then you can pretty easily pass the JSON to a pug page that renders HTML. https://pugjs.org/api/getting-started.html

evdillon
  • 45
  • 6
0

A little but more detail would be helpful, but I think what you need is simply to make a html page and include the js file as a script? If you want to constantly display updated data from a request make a function that updates a div on the html page. Somthing like,

HTML: index.html

...
<head><script src="/path/to/js/file">..</head>
<div id="data"></div>

JS: file.js

var callback = function(data){
 if (data) document.getElementById('data').innerhtml = JSON.stringify(data);
}
var api_call = ajax_request(callback);
  • I'm not using ajax. im using node. i tried to doing that script src , and getting nothing. – Ronnincd Apr 07 '17 at 15:08
  • I don't think youre using node right... this is how you'd do it using just js. If you're using node you need an app.js file and some routes using something like express then simply send the result. Also you don't compile js because its not a compiled language so the way to run it with this method is simply to open the index.html file in your browser. – Daniel Persaud Apr 07 '17 at 18:46
  • also would you consider posting some code? we could probably help you better that way. – Daniel Persaud Apr 07 '17 at 18:48
0

If it is an included JS file in the head, I think @Daniel's answer above should work for you.

If it's a call to API, you can consider using jQuery AJAX call and display the response data in a Div tag Basic Implementation

HTML

<div id="div1"></div>

JS

$.ajax({url: "domain.com/api", success: function(result){
        $("#div1").html(result);
    }});
Arihant
  • 3,847
  • 16
  • 55
  • 86