0

I am trying to consume a RESTful API for JSON data and trying to display it on the HTML page. Here is the code for parsing API into JSON data.

var https = require('https');
var schema;
var optionsget = {
    host : 'host name', // here only the domain name
    port : 443,
    path : 'your url', // the rest of the url with parameters if needed
    method : 'GET' // do GET
};
var reqGet = https.request(optionsget, function(res) {
    console.log("statusCode: ", res.statusCode);
       var chunks = [];
        res.on('data', function(data) {
            chunks.push(data);
        }).on('end', function() {
            var data   = Buffer.concat(chunks);
            schema = JSON.parse(data);
            console.log(schema);
        });

  });
        reqGet.end();
        reqGet.on('error', function(e) {
            console.error(e);
        });
var express = require('express');
var app = express();
app.get('/getData', function (request, response) {
        //console.log( data );
        response.end(schema);
        })

var server = app.listen(8081, function () {
 var host = server.address().address
 var port = server.address().port
 console.log("Example app listening at http://%s:%s", host, port)
})

I am able to get the data in the JSON format but how do I display it in the HTML page? I am trying to do it using node js as I don't want to use jquery for that. Any help would be greatly appreciated. Thanks

AWA
  • 303
  • 1
  • 2
  • 11
  • This may help you https://stackoverflow.com/questions/4083702/posting-a-file-and-associated-data-to-a-restful-webservice-preferably-as-json?rq=1 – aaqib90 Aug 29 '17 at 21:07
  • I think you can use AngularJS to display your Rest data. The sample here is straight forward. https://spring.io/guides/gs/consuming-rest-angularjs/ Hope this help. – Kenny Tai Huynh Aug 29 '17 at 21:07
  • Ya, I can do it using AngularJS but I am trying to do it using node js. Thanks though – AWA Aug 29 '17 at 21:12

1 Answers1

0

your json can render through ejs

npm i ejs

     var app = express();
            app.set('view engine', 'ejs');
            app.use(express.static(__dirname+'/public'));

            app.get('/view/getData', function (request, response) {
             //here view/getData is getData.ejs file in the view folder
             response.render(__dirname+'/public/view/getData'',{schema: schema});
    //schema is the object wich reference through the view in ejs template                    
});

your view file here getData.ejs

//store your getData in view/getData.ejs

<h>  <%= schema[0]%> </h>//your json data here 

basic ejs reference here

res.render brief explanation here