elasticsearch,Javascript,Node.js,Express,elasticsearch" /> elasticsearch,Javascript,Node.js,Express,elasticsearch" />

Javascript 如何使用node.js和express将结果写入文件?

Javascript 如何使用node.js和express将结果写入文件?,javascript,node.js,express,elasticsearch,Javascript,Node.js,Express,elasticsearch,我在elasticsearch的基础上使用node.js和express.js构建了一个应用程序。这是一个带有搜索框的非常简单的应用程序。搜索查询时,它以JSON格式打印结果。例如,如果我搜索关键字“white”,输出如下所示: 现在我想将此结果存储在一个文件中(例如output.json)。这是我的json文件: var fs = require('fs'); var path = "output.json"; var express = require('express'); //To m

我在elasticsearch的基础上使用node.js和express.js构建了一个应用程序。这是一个带有搜索框的非常简单的应用程序。搜索查询时,它以JSON格式打印结果。例如,如果我搜索关键字“white”,输出如下所示:

现在我想将此结果存储在一个文件中(例如output.json)。这是我的json文件:

var fs = require('fs');

var path = "output.json";
var express = require('express'); //To make use of Express' routing capabilities you need to initiate a new Express Router.
var router = express.Router(); //To make use of Express' routing capabilities you need to initiate a new Express Router. get, put, post, delete, all

var searchModule = require('../search_module/search.js');


//There are two ways of using the router methods. You can define one route (for example /home) and attach the methods to it, or you can create a new method for each route.
/* GET home page. */
router.get('/', function(req, res) { //.get() - when you visit a website you make a GET request. You can get data from a URL in a GET request too.
  res.render('index', { title: 'Express' });
});

router.post('/search-results', function(req, res) {//.post() is a method used everywhere for posting data to a server / app. You'll want to use this for submitting forms.
  searchModule.search(req.body, function(data) {
    res.render('index', { title: 'Express', results: data });
  });
});



fs.writeFile(path,data,function(err){
     if(err) console.error(err);
})

module.exports = router;
当我尝试使用node.js的writeFile方法时,我得到一个引用错误,显示“数据”没有定义。我的错误如下所示:

我无法找出这个错误。使用node.js和express是否还有其他方法将输出写入文件

编辑:我编辑了我的javascript

var fs = require('fs');
var path = "output.json";
var express = require('express'); //To make use of Express' routing capabilities you need to initiate a new Express Router.
var router = express.Router(); //To make use of Express' routing capabilities you need to initiate a new Express Router. get, put, post, delete, all

var searchModule = require('../search_module/search.js');


//There are two ways of using the router methods. You can define one route (for example /home) and attach the methods to it, or you can create a new method for each route.
/* GET home page. */
router.get('/', function(req, res) { //.get() - when you visit a website you make a GET request. You can get data from a URL in a GET request too.
  res.render('index', { title: 'Express' });
});

router.post('/search-results', function(req, res) {//.post() is a method used everywhere for posting data to a server / app. You'll want to use this for submitting forms.
  searchModule.search(req.body, function(data) {
    fs.writeFile(path,data,function(err){
       if(err) console.error(err);
    })
    res.render('index', { title: 'Express', results: data });
  });
});
module.exports = router;
但是当我运行这个javascript时,我得到了以下输出:

我没有得到JSON输出。我的输出应该如下所示:

我还在前端(index.ejs)的javascript中使用一个ejs文件,如下所示:

<!DOCTYPE html>
<html>
  <head>
    <title><%= title %></title>

  </head>
  <body>
    <h1><%= title %></h1>
    <form action='/search-results' method='post'>
      <input type="text" name="searchTerm" placeholder="your search term here">
      <button type="submit"> SEARCH </button>
    </form>
    <ul>
      <% if(locals.results) { %>
      <pre>
           <%= JSON.stringify(results,null,2) %>
        </pre>
        <% results.forEach( function( result ) }) %>
      <% } %>
    </ul>
  </body>
</html>
var fileData;

searchModule.search(req.body, function(data) {
    fileData = data;
    res.render('index', { title: 'Express', results: data });
});

fs.writeFile(path,fileData,function(err){
   if(err) console.error(err);
})

搜寻

是否需要从该文件中获取输出?

我正在查看此块:

searchModule.search(req.body, function(data) {
    fs.writeFile(path,data,function(err){
       if(err) console.error(err);
    })
    res.render('index', { title: 'Express', results: data });
});

您已经在代码顶部声明了
path
,但是
data
在此上下文中似乎未定义

此时未定义
数据
变量。 您可以在
searchModule.search
调用中移动
fs.writeFile
函数,如下所示:

<!DOCTYPE html>
<html>
  <head>
    <title><%= title %></title>

  </head>
  <body>
    <h1><%= title %></h1>
    <form action='/search-results' method='post'>
      <input type="text" name="searchTerm" placeholder="your search term here">
      <button type="submit"> SEARCH </button>
    </form>
    <ul>
      <% if(locals.results) { %>
      <pre>
           <%= JSON.stringify(results,null,2) %>
        </pre>
        <% results.forEach( function( result ) }) %>
      <% } %>
    </ul>
  </body>
</html>
var fileData;

searchModule.search(req.body, function(data) {
    fileData = data;
    res.render('index', { title: 'Express', results: data });
});

fs.writeFile(path,fileData,function(err){
   if(err) console.error(err);
})
或者在之前声明变量,并在
searchModule.search
call中将其设置为可在写入文件的范围内显示:


对我已经声明了一个路径(output.json),我想在其中存储我的输出。正如您从我的post方法中所看到的,我想使用该数据来附加到我的文件“output.json”中。因为
数据
在post方法中是有效的,所以您可以1。从post方法或2中的该位置调用
writeFile
。在文件顶部声明一个新变量,该变量存储post方法中的
数据。您可能需要执行以下操作:
JSON.stringify(data)
我还在前端使用一个ejs文件来显示结果。我已经更新了我的帖子以包含该文件。我是否需要从此ejs文件获取结果,因为我在此处使用了JSON.stringify。请尝试将服务器端的
数据
单独保留,并从.ejs文件中尝试
请查看我更新的帖子。我尝试使用您的第一个解决方案,但现在我的“output.json”看起来已经好多了。取决于您的数据,但可能您必须使用类似于
JSON.stringify(data)
的东西将数据转换为纯JSON。我还在前端使用ejs文件来显示结果。我已经更新了我的帖子以包含该文件。我是否需要从这个ejs文件中获取结果,因为我在这里使用了JSON.stringify。