我有一个遗留系统,可以将原始\纯 HTTP 响应作为字符串(包含所有必需的 header + 正文的文本)返回。
出于性能原因,我想直接发送此文本而不进行任何解析修改。
所以目标是代理接收到原始 HTTP 响应。
const express = require('express');
const app = express();
const router = app.Router();
router.get('request',()=>{
const plainTextWithHeadersFromExternalSystem = `HTTP/1.1 200 OK
Date: Sun, 10 Oct 2010 23:26:07 GMT
Server: Apache/2.2.8 (Ubuntu) mod_ssl/2.2.8
OpenSSL/0.9.8g
Last-Modified: Sun, 26 Sep 2010 22:04:35 GMT
ETag: "45b6-834-49130cc1182c0"
Accept-Ranges: bytes
Content-Length: 12
Connection: close
Content-Type: text/html
Hello world!`;
... TODO: send text with headers and body as a response.
});
它可以是任何内容类型,而不仅仅是纯文本。
是否可以使用 Node.js express lib 简单地代理它有什么想法吗?
最佳答案
我自己也遇到了同样的问题,不得不深入研究 Node 的核心 http
和 net
模块来解决这个问题。
要跳过默认的 HTTP 响应行(例如 HTTP/1.1 200 OK
)和 Express/Node.js 发送的所有 header ,您可以发送文本直接通过the response's socket .
const responseMessage = `HTTP/1.1 200 OK
Date: Sun, 10 Oct 2010 23:26:07 GMT
Content-Type: text/html
Content-Length: 13
Hello, World!
`
router.get('/request', (req, res) => {
res.socket.end(responseMessage)
})
关于javascript - Node.js Express 发送原始 HTTP 响应,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62447895/