如何将表单数据从节点服务器传递到extrenal服务器?

时间:2017-07-07 08:06:27

标签: javascript node.js

这是我的代码。如何将表单数据发送到外部服务器?谁能解决我的麻烦? 提前谢谢。

var http = require('http');
var FormData = require('form-data');

var form = new FormData();

form.append('books', ['book1', 'book2']); // my form data

var options = {
  host: 'http://localhost',
  port: 8080,
  path: '/app/updateBooks',
  method: 'POST'
};

http.request(options, function(resp){
    console.log(resp);
}).on("error", function(err){
    console.log(err);
});

1 个答案:

答案 0 :(得分:1)

请求模块可用于向外部服务器发送数据: https://www.npmjs.com/package/request

例如:

const request = require('request')
request({
    uri: 'https://www.example.com/',
    qs: { access_token: token },  // object containing querystring values to be appended to the uri
    method: 'POST',
    json: messageData //json - sets body to JSON representation of value and adds Content-type: application/json header

  }, function (error, response, body) {
    if (!error && response.statusCode == 200) {
        //success condition
    } else {
      // failure condition
    }
  })