簡體   English   中英

使用 Node.js 將來自服務器端 GET 請求的響應傳遞給客戶端

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

我在網頁上有一個按鈕。 單擊按鈕時,服務器端代碼向內部 API 發送 GET 請求。 我無法將響應傳遞回客戶端。 我可以在 Node 中記錄響應 - 但客戶端上的響應為空。 任何幫助,將不勝感激。

客戶端

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));
  }
})

服務器端

#!/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
}

我沒有看到來自服務器的任何響應。 如果你想向用戶發送一些東西,你需要做一個

 res.json({your json data});

這將返回對請求的 JSON 響應,此外您可以設置響應代碼等。

我去了proxim0建議的路線方向。

服務器端

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");
});

客戶端

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)
    
  }
})

這使我可以將服務器端代碼中獲取的數據放入客戶端的瀏覽器控制台中。

感謝您為我指明正確的方向!

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM