简体   繁体   English

使用 Node.js 将来自服务器端 GET 请求的响应传递给客户端

[英]Pass Response from Server Side GET request to client using Node.js

I have a button on a web page.我在网页上有一个按钮。 When the button is clicked server side code sends a GET request to an internal API.单击按钮时,服务器端代码向内部 API 发送 GET 请求。 I am having trouble passing the response back to the client.我无法将响应传递回客户端。 I can log the response in Node - but the response is empty on the client.我可以在 Node 中记录响应 - 但客户端上的响应为空。 Any help would be appreciated.任何帮助,将不胜感激。

Client Side客户端

window.addEventListener('DOMContentLoaded', (event) => {
  console.log('DOM fully loaded and parsed')
  let body = document.querySelector('body')
  let h3 = document.createElement('h3')
  h3.innerText = 'ONT is on Node:'
  body.appendChild(h3)
  let button = document.createElement('button')
  button.innerText = 'Get ONT'
  body.append(button)
  button.addEventListener('click', getONT)

  async function getONT() {
    let response = await (fetch('https://ont.xxx.net/node/', ))
    .then(result => resultBody = JSON.stringify(result))
    .then(result => {console.log(resultBody)})
    .catch(error => console.log('error', error));
  }
})

Server Side服务器端

#!/usr/bin/env nodejs
var http = require('http');
var https = require('https')
var fetch = require('node-fetch')

http.createServer(function (request, response) {
  response.setHeader("Content-Type", "application/json");
  response.setHeader("Access-Control-Allow-Origin", "*");
  response.writeHead(200);
  response.end(JSON.stringify(getData()))
}).listen(8080);
console.log('Server running at http://127.0.0.1:8080/');

async function getData() {
  const smx = "https://xxx/rest/v1/ont/searchall?filter=object.ontId LIKE xxx"
  console.log(smx)
  let response = await fetch(smx, {
    method: 'GET',
    withCredentials: true,
    headers: {
      "Authorization": "Basic xxx",
      "Access-Control-Allow-Origin": "*",
      "Content-Type": "application/json"
    },
    redirect: 'follow'
  })
  let data = await response.json()
  data = JSON.stringify(data)
  console.log(typeof(data))
  return data
}

I don't see any response back from the server.我没有看到来自服务器的任何响应。 If you want to send something back to the user you need to do a如果你想向用户发送一些东西,你需要做一个

 res.json({your json data});

This will return a JSON response to the request, further to that you can set the response code etc.这将返回对请求的 JSON 响应,此外您可以设置响应代码等。

I went the route direction suggested by proxim0.我去了proxim0建议的路线方向。

Server Side服务器端

app.use('/', router);

router.get('/' , function(req,res,next){
  request({
    method: 'GET',
    withCredentials: true,
    headers: {
      "Authorization": "xxx",
      "Access-Control-Allow-Origin": "*",
      "Content-Type": "application/json"
    },
    redirect: 'follow',

    uri:'https://xxx:18443/rest/v1/ont/searchall?filter=object.ontId LIKE xxxxx'
  }).pipe(res)
})

// Server setup
app.listen(8080 , ()=>{
  console.log("server running");
});

Client Side客户端

window.addEventListener('DOMContentLoaded', (event) => {
  console.log('DOM fully loaded and parsed')
  let body = document.querySelector('body')
  let h2 = document.createElement('h2')
  h2.innerHTML
  let h3 = document.createElement('h3')
  h3.innerText = 'ONT is on Node:'
  body.appendChild(h3)
  let button = document.createElement('button')
  button.innerText = 'Get ONT'
  body.append(button)
  button.addEventListener('click', getONT)

  async function getONT() {
    let response = await fetch('https://ont.xxx/node/')
    let data = await response.json()
    console.log(data)
    
  }
})

This allows me to get the data obtained in the server side code into the browser console of the client.这使我可以将服务器端代码中获取的数据放入客户端的浏览器控制台中。

Thanks for pointing me in the right direction!感谢您为我指明正确的方向!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM