如何使用 Node.js 发出外部 HTTP 请求 [英] How to make external HTTP requests with Node.js

查看:40
本文介绍了如何使用 Node.js 发出外部 HTTP 请求的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这个问题很简单.我想使用 Node.js 服务器作为代理来记录、验证 HTTP 查询并将其转发到后端 HTTP 服务器(PUT、GET 和 DELETE 请求).

The question is fairly simple. I want to use a Node.js server as a proxy to log, authenticate, and forward HTTP queries to a backend HTTP server (PUT, GET, and DELETE requests).

我应该为此使用什么库?恐怕我找不到.

What library should I use for that purpose? I'm afraid I can't find one.

推荐答案

NodeJS 支持 http.request 作为标准模块:http://nodejs.org/docs/v0.4.11/api/http.html#http.request

NodeJS supports http.request as a standard module: http://nodejs.org/docs/v0.4.11/api/http.html#http.request

var http = require('http');

var options = {
  host: 'example.com',
  port: 80,
  path: '/foo.html'
};

http.get(options, function(resp){
  resp.on('data', function(chunk){
    //do something with chunk
  });
}).on("error", function(e){
  console.log("Got error: " + e.message);
});

这篇关于如何使用 Node.js 发出外部 HTTP 请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆