[英]how to refer elastic search JavaScript client library in node js controller files
I am doing node.js RESTapi in express framework. 我正在快速框架中执行node.js RESTapi 。 I want to integrate elastic search to my project.
我想将弹性搜索集成到我的项目中。 So I installed elastic search JavaScript client library and add these codes in
app.js
. 因此,我安装了弹性搜索JavaScript客户端库,并将这些代码添加到
app.js
。
var client = new elasticsearch.Client({
host: 'localhost:9200',
log: 'trace'
});
client.ping({
requestTimeout: 30000,
}, function (error) {
if (error) {
console.error('elasticsearch cluster is down!');
} else {
console.log('All is well');
}
});
and I am getting ' All is well '. 我得到“ 一切都很好 ”。 But how do I refer this client in my controller files.
但是如何在控制器文件中引用此客户端。
Passing Arguments : 传递参数:
Pass your variable as arguments to your import function, Like this you can pass variable from one file to another file. 将变量作为参数传递给导入函数,像这样,您可以将变量从一个文件传递到另一个文件。
//app.js
var client = new elasticsearch.Client({
host: 'localhost:9200',
log: 'trace'
});
var catRouters = require("categorry.controller.js")(client);
// categorry.controller.js
module.exports = function (client) {
....
return { create, read, update, readById, categoriesByCity, live_search };
};
Using Global Variables : 使用全局变量:
Set your variable into Node.js global object to use in other files, Node.js global objects are global in nature and available in all modules. 将变量设置为Node.js全局对象以在其他文件中使用,Node.js全局对象本质上是全局的,并且在所有模块中都可用。
//app.js
var client = new elasticsearch.Client({
host: 'localhost:9200',
log: 'trace'
});
global.client = client;
// categorry.controller.js
....
consoel.log(global.client);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.