从restapi检索响应并在UI中显示

时间:2017-07-31 08:11:51

标签: node.js

我正在尝试在nodejs中调用restapi,并且需要在UI中显示整个信息或部分信息,但是我在UI中打印响应时遇到问题,如果您在我的下面的代码中注意到,我可以打印incident_no从htm帖子但我需要打印来自restapi的回复。

    var Client = require('node-rest-client').Client;
var express = require('express');
var app = express();

Object.assign = require('object-assign');

// direct way 
var client = new Client();

var request = require('request')
var username = 'api_batch'
var password = 'batch@2016'


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)

})  


app.use(express.static('public'));  
app.get('/restapi_get.htm', function (req, res) {
   res.sendFile( __dirname + "/" + "restapi_get.htm" );
})


app.get('/get_status', function (req, res) {
   // Prepare output in JSON format

       var options = {
      url: "https://paypal.service-now.com/api/payp2/table/incident/number/" + req.query.incident_no,
      auth: {
        user: username,
        password: password
      }
    }

        response = {
            incident_no:req.query.incident_no,
        };


    request(options, function (err, res, body) {
      if (err) {
        console.dir(err)
        return
      }
      console.dir('headers', res.headers)
      console.dir('status code', res.statusCode)
      console.dir(body)
      console.log(res)


    })




   console.log(response);
   res.end(JSON.stringify(response));
})

   

  <form action = "http://localhost:8081/get_status" method = "GET">
     First Name: <input type = "text" name = "incident_no">  <br>
     <input type = "submit" value = "Submit">
  </form>

0 个答案:

没有答案