这是我第一次在这里发帖提问:我在AngularJS中为前端创建了一个网站,在后端创建了NodeJS。本网站应该向我提供有关弹性搜索群集的信息,从Elasticsearch
索引中获取一些信息。我曾尝试使用Elasticsearch Javascript API
来处理我的请求,但它无法正常工作。
我正在使用ElasticSearch
5.4
以下是请求的示例:
var client = new elasticsearch.Client ({
host: 'https://noev02pe.fr:9200',
auth: 'user:password',
log: 'trace',
headers: {
'Authorization': 'Basic user:password',
}
});
export function connect() {
client.search({
index: 'metric-prod*',
q: 'kafka'
}
, function (error, response) {
console.log(response);
});
}
并且控制台上的响应是:
{ error:
{ root_cause: [ [Object] ],
type: 'security_exception',
reason: 'missing authentication token for REST request [/metric-
prod*/_search?q=kafka]',
header: { 'WWW-Authenticate': 'Basic realm="security" charset="UTF-8"'
} },
status: 401 }
我也尝试过做经典的帖子请求:
export function createUser(request,response,next){
var username = request.params.username;
var userData = querystring.stringify(request.body);
console.log(userData);
var options ={
hostname: 'noev02vr.fr',
port: 9200,
rejectUnauthorized: false,
path: "_xpack/security/user/"+username,
method:'POST',
headers: {
'Authorization': 'Basic ' + prodPass,
'Content-Type': 'application/json',
'Content-Length': userData.length
}
};
var post_req=http.request(options, function(res){
console.log('post user reussi');
res.on('data', function(data){
response.writeHead(res.statusCode);
response.write(data);
console.log(res.statusCode);
});
});
post_req.write(userData);
post_req.end();
}
我得到500错误。
答案 0 :(得分:0)
基本身份验证
const { Client } = require('@elastic/elasticsearch')
const client = new Client({
node: 'https://localhost:9200', //Replace with your URL.
auth: {
username: 'elastic',
password: '*****' //Replace with your password
}
})
否则,您可以在节点URL中提供凭据
const { Client } = require('@elastic/elasticsearch')
const client = new Client({
node: 'https://username:password@localhost:9200'
})
如果启用了ssl,则为配置
const { Client } = require('@elastic/elasticsearch')
const client = new Client({
node: 'https://localhost:9200',
auth: {
username: 'elastic',
password: '*****'
},
ssl: {
ca: fs.readFileSync('./cacert.pem'),
rejectUnauthorized: false
}
})
您可以通过此链接获取用户名和密码
https://www.elastic.co/guide/en/cloud-enterprise/current/ece-password-reset-elastic.html