使用 nodejs 调用 Web 服务

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19392744/
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:00:01  来源:igfitidea点击:

Calling a web service using nodejs

javascriptnode.jshttp

提问by iJade

I'm quite new to Node.js. I was experimenting with how to call a service using NodeJS. Would be helpful if could point out the NodeJS equivalent of the code below:

我对 Node.js 很陌生。我正在试验如何使用 NodeJS 调用服务。如果可以指出以下代码的 NodeJS 等效项会有所帮助:

$.ajax({
  type: "POST",
  url: "/WebServiceUtility.aspx/CustomOrderService",
  data: "{'id': '2'}",
  contentType: "application/json; charset=utf-8",
  dataType: "json",
  success: function (message) {
    ShowPopup(message);
  }
});

Any helpful links would be most appreciated.

任何有用的链接将不胜感激。

采纳答案by hexacyanide

The Node.js equivalent to that code can be using jQuery server-side, using other modules, or using the native HTTP/HTTPSmodules. This is how a POST request is done:

与该代码等效的 Node.js 可以使用 jQuery 服务器端、使用其他模块或使用本机HTTP/ HTTPS模块。这是 POST 请求的完成方式:

var http = require('http');
var data = JSON.stringify({
  'id': '2'
});

var options = {
  host: 'host.com',
  port: '80',
  path: '/WebServiceUtility.aspx/CustomOrderService',
  method: 'POST',
  headers: {
    'Content-Type': 'application/json; charset=utf-8',
    'Content-Length': data.length
  }
};

var req = http.request(options, function(res) {
  var msg = '';

  res.setEncoding('utf8');
  res.on('data', function(chunk) {
    msg += chunk;
  });
  res.on('end', function() {
    console.log(JSON.parse(msg));
  });
});

req.write(data);
req.end();

This example creates the data payload, which is JSON. It then sets up the HTTP post options, such as host, port, path, headers, etc. The request itself is then set up, which we collect the response for parsing. Then we write the POST data to the request itself, and end the request.

此示例创建数据负载,即 JSON。然后设置 HTTP post 选项,例如主机、端口、路径、标头等。然后设置请求本身,我们收集响应以进行解析。然后我们将 POST 数据写入请求本身,并结束请求。

回答by Chris Tavares

The easiest way at the moment is to use the Request module. See the page there for lots of examples showing how to do what you want.

目前最简单的方法是使用Request 模块。请参阅那里的页面以获取大量显示如何做你想做的事的例子。

If you want to use raw node.js, you'll need to use either httpor httpsbuilt-in modules, but you'll have to handle a lot of the encoding and streaming details yourself. Also, be sure to look specifically at the client parts of the documentation, not the server.

如果您想使用原始 node.js,您需要使用httphttps内置模块,但您必须自己处理许多编码和流媒体细节。另外,一定要专门查看文档的客户端部分,而不是服务器。

回答by Akash Khapare

//--------- Tracking request service                  
factory.trackRequest = function (payload) {
    return $http({
        method: 'POST',
        **url: 'https://uat-userauthentication.bdt.kpit.com/'+ 
 'employee/trackRequestStatus'**,
        data: payload
    });
 };

 return factory;

I call node js service in angular by UI routing and I used trackRequestfunction in controller.

我通过 UI 路由以角度调用节点 js 服务,并trackRequest在控制器中使用函数。