【问题标题】:Call rest service in node server在节点服务器中调用 rest 服务
【发布时间】:2013-10-31 05:45:33
【问题描述】:

我已经像这样在 Node 服务器中创建了一个 restfull 服务

    express().use('/getdata', express()
            .get('/', function (req, res) {
                //....                  
            })
            .put('/', function (req, res) {
                //....
            })
       );

当我从前端/浏览器对 http://localhost/getdata 执行 GET 或 PUT 时,它工作正常。

那么如何在节点服务器中做同样的事情,基本上,使用HTTP对象在节点服务器中发出一个get请求。 url 路径会是怎样的?

【问题讨论】:

  • 非常整洁,您可以像这样将快速路线链接在一起!

标签: javascript node.js http express


【解决方案1】:

网址与您的浏览器相同:

var http = require('http');

http.get('http://localhost/getdata/', function(res) {
  ...
});

如果您想发出PUT 请求,请改用http.request

http.request({
  path   : '/getdata/',
  method : 'PUT',
  ...
}, function(res) {
  ...
});

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-07-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多