node.js 如何将 post 请求从 express.js 发送到另一台服务器(java)?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20526757/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 16:29:40  来源:igfitidea点击:

how to send Post request from express.js to another server ( java)?

node.jspostrequest

提问by silvesterprabu

I have to send data (json object) to another webserver (java).

我必须将数据(json 对象)发送到另一个网络服务器(java)。

This is my node.js code

这是我的 node.js 代码

var express = require('express');

var app = express();

app.get('/', function (req, res) {
    var data = querystring.stringify({
        username: "myname",
        password: " pass"
    });

    var options = {
        host: 'www.javaserver.com',
        port: 8070,
        path: '/login',
        method: 'POST',
        headers: {
            'Content-Type': 'application/x-www-form-urlencoded',
            'Content-Length': Buffer.byteLength(data)
        }
    };

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

});

app.listen(8090);

This is not working. How can I do this?

这是行不通的。我怎样才能做到这一点?

回答by vmx

You are repeating req, and res variables for the post request. I have updated your code and tested it working with requestb.in

您正在为 post 请求重复 req 和 res 变量。我已经更新了您的代码并使用 requestb.in 对其进行了测试

var express = require('express');
var querystring = require('querystring');
var http = require('http');

var app = express();
app.get('/', function (req, res) {
  var data = querystring.stringify({
    username: "myname",
    password: " pass"
  });

  var options = {
    host: 'requestb.in',
    port: 80,
    path: '/nfue7rnf',
    method: 'POST',
    headers: {
      'Content-Type': 'application/x-www-form-urlencoded',
      'Content-Length': Buffer.byteLength(data)
    }
  };

  var httpreq = http.request(options, function (response) {
    response.setEncoding('utf8');
    response.on('data', function (chunk) {
      console.log("body: " + chunk);
    });
    response.on('end', function() {
      res.send('ok');
    })
  });
  httpreq.write(data);
  httpreq.end();
});

app.listen(8090);

Please update the request host and path in the code to the values you need. Let me know if it still doesn't work for you.

请将代码中的请求主机和路径更新为您需要的值。如果它仍然不适合您,请告诉我。

回答by Nitin...

Please list exact error, "this is not working..." is not very helpful to identify the issue. The code is moreover fine with minor issues.

请列出确切的错误,“这不起作用......”对于确定问题不是很有帮助。代码也很好,有一些小问题。

var http = require("http");
var querystring = require("querystring");
var express=require('express');
var app=express();
app.get('/',function(req, res) {
    var data = querystring.stringify({
        username: "myname",
        password: " pass"
    });

    var options = {
        host: 'www.javaserver.com',
        port: 8070,
        path: '/login',
        method: 'POST',
        headers: {
            'Content-Type': 'application/x-www-form-urlencoded',
            'Content-Length': Buffer.byteLength(data)
        }
    };

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

The only thing to care about is, there should be a server at www.javaserver.com:8070 to give response for /login for data being POST'ed in this case the login credentials.

唯一需要关心的是,在 www.javaserver.com:8070 上应该有一个服务器来响应 /login 的数据,在这种情况下是登录凭据。