将nodejs中的变量和数据转换为html文件

时间:2017-07-11 09:44:00

标签: html node.js elasticsearch

我是nodejs的新手,我使用nodejs连接到elasticsearch并从elasticsearch获取数据,操纵它然后将数据放入html文件,但问题是html是客户端和nodejs是服务器端,我不知道如何连接它。 这是我的代码: node.js:

var counting = 1;
var total = 1;
var elasticsearch = require('C:/Windows/System32/node_modules/elasticsearch');
    var client = new elasticsearch.Client({
        host: 'localhost:9200'
    });

     client.count({index: 'csv_pattern'}, function(err, resp, status) {  
        counting = resp['count'];
    });

        client.search({  

            index: 'csv_pattern',
            body: {
                size : counting
            }
        },
            function (error, response, status) {
                total = response['hits']['total'];    
              });

我想将node.js文件的变量用于html文件,请告诉我该怎么做。 非常感谢您的帮助。

1 个答案:

答案 0 :(得分:0)

使用可以在节点中使用EJS,首先安装并将视图引擎设置为ejs,只需将html代码保存在output.ejs文件而不是output.html中,并使用render函数将输出发送到html,如下所示。

app.set('view engine', 'ejs');
response.render('output.ejs', {result: JSON.parse(response_data)});

在ejs文件中

<tbody>

         <% for(var i=0; i<result.length; i++) {%>
         <tr>
                <td><%= result[i].paramname %></td>
        </tr>
  <% } %>

 </tbody>
相关问题