簡體   English   中英

在 Node HTTP2 流中發送和接收 JSON

[英]Sending and receiving JSON in Node HTTP2 streams

我正在使用 HTTP2 在 Node 中構建身份驗證微服務。

如何正確地將 JSON 寫入 Node HTTP2 流並從中讀取它?

文檔給出了這些示例:

const http2 = require('http2');
const fs = require('fs');

const server = http2.createSecureServer({
  key: fs.readFileSync('localhost-privkey.pem'),
  cert: fs.readFileSync('localhost-cert.pem')
});
server.on('error', (err) => console.error(err));

server.on('stream', (stream, headers) => {
  // stream is a Duplex
  stream.respond({
    'content-type': 'text/html',
    ':status': 200
  });
  stream.end('<h1>Hello World</h1>');
});

server.listen(8443);

const http2 = require('http2');
const fs = require('fs');
const client = http2.connect('https://localhost:8443', {
  ca: fs.readFileSync('localhost-cert.pem')
});
client.on('error', (err) => console.error(err));

const req = client.request({ ':path': '/' });

req.on('response', (headers, flags) => {
  for (const name in headers) {
    console.log(`${name}: ${headers[name]}`);
  }
});

req.setEncoding('utf8');
let data = '';
req.on('data', (chunk) => { data += chunk; });
req.on('end', () => {
  console.log(`\n${data}`);
  client.close();
});
req.end();

假設我有一個JSON對象,我想將它寫入客戶端中的流並從服務器中的流中讀取,反之亦然。 我該如何正確執行此操作?

我可以將我的JSON字符串化為str並使用request.write(str, 'utf-8) ,但這是最佳方式嗎? 我如何在另一端監聽 JSON 以處理它?

服務器...


const http2 = require('http2');
const fs = require('fs');

const server = http2.createSecureServer({
  key: fs.readFileSync('localhost-privkey.pem'),
  cert: fs.readFileSync('localhost-cert.pem')
});

server.on('error', (err) => {
  console.error(err)
});

server.on('stream', (stream, headers) => {
  // stream is a Duplex
  
  // headers[:path] is the normal request from browser. A path url.

  console.log("Request from client: " + headers[:path]);
  

  req.on("data", (data) => {
    console.log("Data from HTTPS2 client(event): " + data);
    // It is just the data from 'POST' method.
  });



stream.respond({
    'content-type': 'text/html',
    ':status': 200
  });

  stream.end('<h1>Hello World</h1> Or a string, other data, wherever you want...');

});

server.listen(8443);

客戶端。 可以是“GET”或“POST”方法中的 XHR 請求。 服務器端是一樣的。 要使用“GET”模式,請使用 method = GET 並刪除“req.end('XXX any string.bla, bla, bla');”

const http2 = require('http2');
const fs = require('fs');


const client = http2.connect('https://localhost:8443', {
  ca: fs.readFileSync('localhost-cert.pem')
});


client.on('error', (err) => {
  console.error(err)
});

//':method': 'GET/POST' Can be just one.
const req = client.request({ ':method': 'POST', ':path': '/' });

req.on('response', (headers, flags) => {
  for (const name in headers) {
    console.log(`${name}: ${headers[name]}`);
  }
});

req.setEncoding('utf8');
let data = '';

//Data from HTTPS2 server.
req.on('data', (chunk) => { 

  data += chunk; 

});

req.on('end', () => {
  console.log(`\n${data}`);
  client.close();
});

//Only for POST method.
req.end("The data you want to send. String, JSON, buffer whatever...");

暫無
暫無

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

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