0

我正在尝试使用 Elasticsearch 创建 NodeJS 应用程序

这是我的代码

<!DOCTYPE html>
<script src="../bower_components/elasticsearch/elasticsearch.js"></script>
<script>
function esPinger() {
    var elasticsearch = require('elasticsearch');
var client = elasticsearch.Client({
  host: 'localhost:9200',
  log: 'trace'
});

client.ping({
  // ping usually has a 3000ms timeout
  requestTimeout: Infinity,

  // undocumented params are appended to the query string
  hello: "elasticsearch!"
}, function (error) {
  if (error) {
    console.trace('elasticsearch cluster is down!');
  } else {
    console.log('All is well');
    alert("YAY");
  }
});
}
</script>
<html>

  <head>
    <title>NodeJS Starter Application</title>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <link rel="stylesheet" href="stylesheets/style.css">
  </head>

  <body>

    <center>
    <button onclick="esPinger()">Ping ES</button>
    </center>
  </body>

</html>

我将elasticsearch客户端安装为

npm install elasticsearch 

并且

bower install elasticsearch 

在我的项目文件夹中,我可以看到有一个bower_componenets文件夹包含用于 elasticsearch 的所有 .js 文件,包括elasticsearch.js

现在,当我运行我的页面并单击按钮时,我得到

Uncaught ReferenceError: elasticsearch is not defined

知道为什么吗?我错过了一些配置吗?

供参考,这是我的项目结构的样子

在此处输入图像描述

编辑

这是我在检查元素控制台中看到的

在此处输入图像描述

怎么可能没有图书馆!

4

1 回答 1

0

Node.JS 是一种服务器端技术,而不是浏览器技术。因此,特定于节点的调用,如

var elasticsearch = require('elasticsearch');

不在浏览器中工作。

于 2018-01-12T06:54:55.837 回答