Node.js的本地主机负载和负载,但不工作
我想打电话从这个节点API搜索引擎,但由于某种原因,节点,当我把“/搜索”到的网址无法正常工作。我想没有API的调用client.search和它的作品。谢谢你的任何输入!
const express = require('express');
const bodyParser = require('body-parser');
const PORT = 4000;
var client = require ('./connection.js');
var argv = require('yargs').argv;
var getJSON = require('get-json');
let app = express();
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ exptended: true }));
app.get("/search", function results(request, response) {
client.search({
index: 'club',
type: 'clubinfo',
body: {
query: {
match: { "name": "Italian club" }
},
}
},function (error, response,status) {
if (error){
console.log("search error: "+error)
}
else {
console.log("--- Response ---");
console.log(response);
console.log("--- Hits ---");
response.hits.hits.forEach(function(hit){
console.log(hit);
})
}
});
});
app.listen(PORT, () => console.log('wowzers in me trousers, Listening on port ' + PORT));
回答如下:
- 你重写
response
变量。使用不同的名称为快速响应,并从client.search
发回的响应 - 实际上,你需要发回的响应。要做到这一点呼叫
response.send({ // somethingHere })
。
在你的情况下,它可能是这样的
app.get("/search", function (request, response) {
client.search({
index: 'club',
type: 'clubinfo',
body: {
query: {
match: { "name": "Italian club" }
},
}
},function (error, data, status) {
if (error){
console.log("search error: "+error);
// Send back an error resposne
response.status(500).send(error);
}
else {
console.log("--- Response ---");
console.log(data);
console.log("--- Hits ---");
data.hits.hits.forEach(function(hit){
console.log(hit);
})
// Send back the response
response.send(data);
}
});
});
与本文相关的文章
评论列表(0)
- 暂无评论