最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Node.js的本地主机负载和负载,但不工作

运维笔记admin11浏览0评论

Node.js的本地主机负载和负载,但不工作

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)

  1. 暂无评论