您可以从其他服务器发送HTTP响应吗?

编程入门 行业动态 更新时间:2024-10-14 12:24:32
本文介绍了您可以从其他服务器发送HTTP响应吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

傻问题.

我最近一直在使用Node.js,并且喜欢设置服务器和发出请求等简单的事情.我还没有尝试过,但是想知道如何将数据从一个请求转发到另一台服务器,并让第二台服务器将响应发送到客户端.

I've been playing with Node.js lately, and like how easy it is to set up servers and make requests etc. I haven't tried yet, but was wondering how I might forward data from one request to another server, and have that second server send response to the client.

这可能吗?

CLIENTX->服务器A->服务器B->客户X

CLIENTX -> SERVER A -> SERVER B -> CLIENT X

让我困惑的是如何发送给同一客户?该信息应该出现在请求标头中,尽管不是吗?将信息转发到服务器B只是一个问题?

Whats confusing to me is how to send to same client? This information should be present in the request header though no? Is it a matter of forwarding that information to SERVER B?

我处于一种在Node.js服务器上接受请求的情况下,想将一些数据转发到我创建的Laravel API并将响应发送到那里的客户端表单.

I am in a situation where I am accepting requests on a Node.js server, and would like to forward some of the data to a Laravel API I have created and send response to client form there.

欣赏您的答案,

马特

推荐答案

使用 请求 模块.

这是服务器A"的示例实现,它将所有请求原样传递到服务器B,然后将其响应发送回客户端:

Here's an example implementation for "Server A", that would pass all requests to Server B as-is, and send back its responses to the client:

'use strict'; const http = require('http'); const request = require('request').defaults({ followRedirect : false, encoding : null }); http.createServer((req, res) => { let endpoint = 'server-b.example' + req.url; req.pipe(request(endpoint)).pipe(res); }).listen(3000);

您也可以使用 http 模块,但是 request 使其更容易.

Instead of request you could also implement this with the http module, but request makes it easier.

任何对 server-a.example/some/path/here 的请求将通过相同的路径(+方法,查询字符串,主体数据)传递给服务器B, 等等).

Any requests to server-a.example/some/path/here will be passed to Server B, with the same path (+ method, query strings, body data, etc).

followRedirect 和 encoding 是两个有用的选项.在此处记录.

followRedirect and encoding are two options that I found useful when passing requests to other servers like this. They are documented here.

更多推荐

您可以从其他服务器发送HTTP响应吗?

本文发布于:2023-10-11 14:18:41,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1481947.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:您可以   服务器   HTTP

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!