简体   繁体   中英

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.

I have heard about http request but i'm not sure if i'm using it right.

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?

I have control of both servers and they are different domains

Server A index to send post:

<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:

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

sereverB:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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