0
我有一個elasticsearch(可以是任何東西)js文件,它執行簡單的查詢並使用promise來返回數據。Module Return Promise to Express
我調用模塊中我的快件(server.js)希望得到的數據,因爲最終我想將它發送到角或任何真的...
我想知道爲什麼我不取回數據中快遞服務器?
get.js[我elasticsearch js文件]
var elasticsearch = require('elasticsearch');
var client = new elasticsearch.Client({
host: 'localhost:9200',
});
var Q = require('q')
function GetElastic(qry) {
var deferred = Q.defer();
client.search({
index: 'social',
type: 'network',
body: {
query: {
query_string:{
query:qry
}
}
}
}).then(function (resp) {
deferred.resolve(resp);
}, function (err) {
console.log(err.message);
return deferred.resolve(err);
});
return deferred.promise;
}
module.exports = GetElastic
server.js[我的Express服務器文件]
var express = require('express')
var bodyParser = require('body-parser')
var Get = require('./models/get')
var app = express()
app.use(bodyParser.json())
app.get('/api/posts',function(req, res){
var qry = 'node'
var get = new Get(qry)
res.writeHead(200, {'Content-Type': 'text/plain'});
res.end(JSON.stringify(get));
console.log(get)
})
app.listen(3000, function(){
console.log('Server listening on',3000)
})
歡迎您 – maddygoround