263790
官方文档提供了一个http代理的例 子。 有什么方法可以让Express使用代理?直接直接配置app.use (req, res, next) 来让所有的请求都使用代理?
require('http').createServer(function(request, response) {
response.writeHead(200, {"Content-Type": "text/html"});
var auth = 'Basic ' + new Buffer("用户名:密码").toString('base64'); // 这里用户名密码请查看“扩展服务”-> “网页代理”
var options = {
host: '10.242.175.127', // 这里查看“扩展服务”-> “网页代理”中的“网页代理地址”
port: 3128,
method:"GET",
path: 'http://taobao.com/index.htm', // 这里填写你要请求的url
headers:{
"Proxy-Authorization": auth,
Host: "taobao.com" // 这里填写你要请求的目标域名
}
};
var body = "";
require("http").get(options, function(res) {
res.on('data', function(chunk) {
body += chunk.toString(); // 返回请求的内容
});
res.on('end', function() {
response.end(body); // 输出请求的内容
});
});
}).listen();
但是我使用的是Express,然后我使用了nodemailer来发送邮件。这个我如何去代理?
mailOptions =
from: userName,
to: mykarAddress,
subject: subeject,
html: content
smtpTransport.sendMail mailOptions, (err, response) ->
if err
return res.send err
console.log(response)
res.send("OK")
根据文档我在app.js里面尝试这样设置请求头,想让所有的请求都是设置了代理,但是没有成功。有什么方法让
app = express()
app.use (req, res, next) ->
req.headers["Proxy-Authorization"] = auth
req.headers.host = "proxy.ace.aliyun.com:3128"
req._parsedUrl.host = "proxy.ace.aliyun.com"
req._parsedUrl.port = 3128
req._parsedUrl.auth = auth
console.log req.headers
console.log "host =>", req.host
console.log "port =>", req.port
next()