我正在尝试通过节点http模块向本地运行的etcd实例发出het请求。
代码看起来像这样
'use strict';
const express = require('express');
const app = express();
var http = require('http');
const port = 10111;
var encoded_url = encodeURI('/v2/keys/message -X GET');
var options = {
host: 'http://127.0.0.1:2379',
path: encoded_url
};
var callback = function (response) {
var str = '';
//another chunk of data has been recieved, so append it to `str`
response.on('data', function (chunk) {
str += chunk;
});
//the whole response has been recieved, so we just print it out here
response.on('end', function () {
console.log(str);
});
}
http.request(options, callback).end();
app.listen(port, () => {
console.log("server started on port " + port);
});
但是我收到以下错误
Error: getaddrinfo ENOTFOUND http://127.0.0.1:2379 http://127.0.0.1:2379:80
at errnoException (dns.js:28:10)
at GetAddrInfoReqWrap.onlookup [as oncomplete] (dns.js:76:26)
如果我从终端发出相同的卷曲请求,我会得到结果
curl http://127.0.0.1:2379/v2/keys/message -X GET
无法弄清问题是什么。
答案 0 :(得分:1)
默认情况下http.request()
使用端口80
。
请改用:
var options = {
protocol: 'http:',
host: '127.0.0.1',
port: 2379,
path: encoded_url
};