node.js - 制作一个 nodejs 网页,该网页将从 elasticsearch 获取数据并在 web 上显示
问题描述
我第一次在 nodejs 所以请提供一些帮助。我在 localhost 上运行了 elasticsearch(http://localhost:9200) 和 kibana(http://localhost:5601)。在弹性搜索中,我有正在访问的索引出勤率。我想制作 nodejs api,它将从这个 elasticsearch 服务器获取数据并将其显示在浏览器上。我使用了以下代码:
var http = require('http');
const { Client } = require('@elastic/elasticsearch')
const client = new Client({ node: 'http://localhost:9200' })
var events = require('events');
var url = require('url');
var eventEmitter = new events.EventEmitter();
http.createServer(function (req, res) {
res.writeHead(200, {'Content-Type': 'text/html'});
res.write(req.url);
eventEmitter.emit(req.url);
eventEmitter.on('/connection/', connect_new);
res.end();
}).listen(8080);
var connect_new=async function run () {
await client.search({
index: 'attendance',
body:{
"aggs": {
"3": {
"terms": {
"field": "timevar.keyword",
"order": {
"_count": "desc"
},
"size": 8
},
"aggs": {
"4": {
"terms": {
"field": "type.keyword",
"order": {
"_count": "desc"
},
"size": 5
}
}
}
}
},
"size": 0,
"fields": [
{
"field": "@timestamp",
"format": "date_time"
},
{
"field": "date",
"format": "date_time"
},
{
"field": "in_timestamp",
"format": "date_time"
}
],
"script_fields": {},
"stored_fields": [
"*"
],
"runtime_mappings": {},
"_source": {
"excludes": []
},
"query": {
"bool": {
"must": [],
"filter": [
{
"match_all": {}
},
{
"range": {
"in_timestamp": {
"gte": "2021-07-13T07:18:27.397Z",
"lte": "2021-07-13T08:22:57.064Z",
"format": "strict_date_optional_time"
}
}
}
],
"should": [],
"must_not": []
}
}
}}).then(function(resp) {
//resp.writeHead(200, {'Content-Type': 'text/html'});
//resp.write('time interval:- ' + resp.body.aggregations['3']["buckets"][0]["key"]);
//resp.write('total students:- ' + resp.body.aggregations['3']["buckets"][0]["doc_count"]);
console.log(resp);
console.log('time interval:- ' + resp.body.aggregations['3']["buckets"][0]["key"]);
console.log('total students:- ' + resp.body.aggregations['3']["buckets"][0]["doc_count"]);
console.log('time interval:- ' + resp.body.aggregations['3']["buckets"][1]["key"]);
console.log('total students:- ' + resp.body.aggregations['3']["buckets"][1]["doc_count"]);
//res.end();
}, function(err) {
console.trace(err.message);
});
}
它在命令行中为我提供以下输出:
{
body: {
took: 4,
timed_out: false,
_shards: { total: 1, successful: 1, skipped: 0, failed: 0 },
hits: { total: [Object], max_score: null, hits: [] },
aggregations: { '3': [Object] }
},
statusCode: 200,
headers: {
warning: '299 Elasticsearch-7.13.3-5d21bea28db1e89ecc1f66311ebdec9dc3aa7d64 "Elasticsearch built-in security features are not enabled. Without authentication, your cluster could be accessible to anyone. See https://www.elastic.co/guide/en/elasticsearch/reference/7.13/security-minimal-setup.html to enable security."',
'content-type': 'application/json; charset=UTF-8',
'content-length': '827'
},
meta: {
context: null,
request: { params: [Object], options: {}, id: 2 },
name: 'elasticsearch-js',
connection: {
url: 'http://localhost:9200/',
id: 'http://localhost:9200/',
headers: {},
deadCount: 0,
resurrectTimeout: 0,
_openRequests: 0,
status: 'alive',
roles: [Object]
},
attempts: 0,
aborted: false
}
}
time interval:- INTERVAL_01-02
total students:- 54
time interval:- INTERVAL_12-01
total students:- 13
但我想在网络浏览器中输出这个 JSON。现在我只得到低于输出的地方:
/connection/
请给我一个可能的解决方案。任何建议表示赞赏。
解决方案
我发现我的概念有点弱。但是在使用中间件后它按预期工作。并向浏览器提供输出。
const elasticsearch = require('elasticsearch');
const client = new elasticsearch.Client({hosts: [ 'http://localhost:9200']});
const express = require( 'express' );
const Confirm = require('prompt-confirm');
const app = express();
app.set( 'port', process.env.PORT || 3002 );
app.get('/',check, for_serach,function (req, res){
let total=req.total_v;
let values=req.data;
const confirm = new Confirm(total+' Records found.Want to see ?')
.ask(function(answer) {
if (answer){
res.send(values);
}
else{
res.send(total+' Records found.Not showing due to selecting no in prompt');
}
});
})
app.get('/delete/',check,for_serach,delete_prompt, function (req, res){
let body = {
"size":6500,
"query": {
"range": {
"in_timestamp": {
"gte": req.start_date,
"lte": req.end_date
}
}
}
}
client.deleteByQuery({index:'attendance',body:body})
.then(results => {
res.json(results);
})
.catch(err=>{
console.log(err)
res.send([]);
});
})
function delete_prompt(req,res,next){
let total=req.total_v;
const confirm = new Confirm(total+' Records found.Confirm for delete :')
.ask(function(answer) {
if (answer){
next()
}
else{
res.send(total+' Records found.Not showing due to selecting no in prompt');
}
});
}
function for_serach(req, res,next){
console.log(req.start_date);
console.log(req.end_date);
let body = {
"size":6500,
"query": {
"range": {
"in_timestamp": {
"gte": req.start_date,
"lte": req.end_date
}
}
}
}
client.search({index:'attendance',body:body})
.then(results => {
req.total_v= results.hits.total.value;
req.data=results.hits.hits;
next()
})
.catch(err=>{
console.log(err)
res.send([]);
});
}
function check(req,res,next){
let start =req.query.start.toString();
let end =req.query.end.toString();
let nstart = start.replace(" ", "+");
let nend = end.replace(" ", "+");
let s_date=new Date(nstart);
let s_end=new Date(nend);
let c_date=new Date();
req.start_date=nstart;
req.end_date=nend;
c_date.setMonth(c_date.getMonth() - 2);
if(s_date>c_date || s_end>c_date){
res.send('wrong date:give date prior to 2 month of today');
}
else if(s_date>=s_end){
res.send('End date need to be after start date');
}
else
{
next()
}
}
app .listen( app.get( 'port' ), function(){
console.log( 'Express server listening on port ' + app.get( 'port' ));
} );
推荐阅读
- javascript - 如何在javascript中使用values data-num html属性
- typescript - 条件返回类型
- build - Azure DevOps:执行期间在任务名称中显示参数值
- laravel - 如何在 Laravel Eloquent 中急切加载时获取多态关系类型?
- django - 如何修复 LNK1104:无法在 C++ 之外打开文件“MSVCRT.lib”
- symfony - Symfony 4 删除 cookie
- notepad++ - 查找并连接多行中的行
- reactjs - React-bootstrap,如何关闭下拉菜单
- python - How to perform OpenGL rotation in more than 1 axis using shaders, pygame, and pyopengl
- excel - 如何根据单元格值对行进行分组 - Excel VBA