Olá, estou tentando fazer o curso de node usando o elasticsearch como banco, porém estou com problema ao isolar a conexão em um novo arquivo.
connectionFactory.js
var elasticsearch = require('elasticsearch');
module.exports = function(){
return elasticsearch.createConnection({
host: 'http://localhost:9200'
});
}
pessoas.js
var connectionFactory = require('../infra/connectionFactory');
module.exports = function(app) {
app.get('/pessoas', function(req, res){
var connection = connectionFactory();
client.search({
index: 'catalogo',
type: 'pessoas',
body: {
query: {
query_string:{
query:"Futebol"
}
}
}
}).then(function (resp) {
var hits = resp.hits.hits;
console.log(hits);
// res.send(JSON.stringify(hits));
res.render('pessoas/lista',{lista:hits});
}, function (err) {
console.log(err.message);
});
function closeConnection() {
client.close();
}
});
}
o servidor esta rodando ok, porem quando acesso no navegador esta me dando o erro, TypeError: elasticsearch.createConnection is not a function at module.exports (C:\Users\phsil\Desktop\Alura\Node.js\Dojo\app\infra\connectionFactory.js:3:23) at C:\Users\phsil\Desktop\Alura\Node.js\Dojo\app\routes\pessoas.js:5:22 at Layer.handle [as handle_request] (C:\Users\phsil\Desktop\Alura\Node.js\Dojo\node_modules\express\lib\router\layer.js:95:5) at next (C:\Users\phsil\Desktop\Alura\Node.js\Dojo\node_modules\express\lib\router\route.js:137:13) at Route.dispatch (C:\Users\phsil\Desktop\Alura\Node.js\Dojo\node_modules\express\lib\router\route.js:112:3) at Layer.handle [as handle_request] (C:\Users\phsil\Desktop\Alura\Node.js\Dojo\node_modules\express\lib\router\layer.js:95:5) at C:\Users\phsil\Desktop\Alura\Node.js\Dojo\node_modules\express\lib\router\index.js:281:22 at Function.process_params (C:\Users\phsil\Desktop\Alura\Node.js\Dojo\node_modules\express\lib\router\index.js:335:12) at next (C:\Users\phsil\Desktop\Alura\Node.js\Dojo\node_modules\express\lib\router\index.js:275:10) at expressInit (C:\Users\phsil\Desktop\Alura\Node.js\Dojo\node_modules\express\lib\middleware\init.js:40:5)