简体   繁体   English

使用 http 请求将 html-form 从服务器发送到另一个 node.js 服务器

[英]Send html-form from server to another node.js server using http request

Hello i started to learn node.js today and i was just wondering if it's possible to send data from a html-form to node.js server than have that server send the data to another node.js server.您好,我今天开始学习 node.js,我只是想知道是否可以将数据从 html 表单发送到 node.js 服务器,而不是让该服务器将数据发送到另一个 node.js 服务器。

I have heard about http request but i'm not sure if i'm using it right.我听说过 http 请求,但我不确定我是否正确使用它。

I thought if i sent the html-form post to server A and than run http request inside post in server A it would send the data to server B post?我想如果我将 html-form 帖子发送到服务器 A,然后在服务器 A 的帖子中运行 http 请求,它会将数据发送到服务器 B 帖子吗?

I have control of both servers and they are different domains我控制了两台服务器,它们是不同的域

Server A index to send post:服务器 A 发送帖子的索引:

<form action="https://serverA.com/sendInfo" method="POST">
  firstname: <input type="text" name="firstname" /><br/>
  lastname: <input type="text" name="lastname" /><br/>
  <button type="submit">Send</button>
</form>

server A:服务器 A:

const express = require("express");
const bodyParser = require('body-parser');
const http = require("http");
const app = express();

app.use(express.static("public"));
app.use(bodyParser.urlencoded({ extended: true }));

app.get("/", (request, response) => { response.sendFile(__dirname + "/views/index.html"); });

app.post('/sendInfo', async (req, res) => {
  try {
    console.log(`Attempting to send firstname: ${req.body.firstname} and lastname: ${req.body.lastname}.`);
    await res.send(`Attempting to send firstname: ${req.body.firstname} and lastname: ${req.body.lastname}.`);

    var postData = JSON.stringify(req.body);

    var myRequest = http.request({
      hostname: "serverB.com",
      path: "/receiveInfo",
      method: "POST",
      headers: {
        "Content-Type": "application/json",
        "Content-Length": Buffer.byteLength(postData)
      }
    });

    myRequest.write(postData);
    myRequest.end();

    myRequest.on('error', function(e) {
      console.error('myRequest error', e);
    });

  } catch (e) {
    console.error("error", e);
  }
});

const listener = app.listen(process.env.PORT, () => {
  console.log("Your app is listening on port " + listener.address().port);
});

And on server B i received nothing and i'm not getting any errors on both servers在服务器 B 上,我什么也没收到,两台服务器上都没有收到任何错误

sereverB:服务器B:

const express = require("express");
const bodyParser = require('body-parser');
const app = express();

app.use(express.static("public"));
app.use(bodyParser.urlencoded({ extended: true }));

app.get("/", (request, response) => { response.sendFile(__dirname + "/views/index.html"); });

app.post("/receiveInfo", async (req, res) => {
  try {
    console.log(req.body)
  } catch (e) {
    console.error("error", e);
  }
});
    
const listener = app.listen(process.env.PORT, () => {
  console.log("Your app is listening on port " + listener.address().port);
});

What am i doing wrong / not understanding?我在做什么错/不理解?

Sorry my english is not that good对不起,我的英语不是那么好

var req = http.request(myRequest, function (res) {
        res.setEncoding('utf8');
        res.on('data', function (chunk) {
            console.log("body: " + chunk);
        });
    });
    req.write(data);
    req.end();

Try adding this.. before request write尝试添加这个..在请求写入之前

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

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