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

标签 javascript node.js express elasticsearch

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

现在我想将这个结果存储在一个文件中(例如 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 方法时,我收到一个引用错误,显示“数据”未定义。我的错误如下所示:http://i.stack.imgur.com/lwXfW.png

我无法找出这个错误。有没有其他方法可以使用 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 时,我得到了这个输出: http://i.stack.imgur.com/rfBq0.png

我没有得到 JSON 输出。我的输出应如下所示:http://i.stack.imgur.com/VHuWl.png

我还在前端使用带有 javascript 的 ejs 文件 (index.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>

我需要从这个文件中获取输出吗?

最佳答案

此时未定义data 变量。 您可以像这样在 searchModule.search 调用中移动您的 fs.writeFile 函数:

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

或者之前声明你的变量并在 searchModule.search 调用中设置它,以便在写入你的文件的范围内之后不负责任:

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);
})

关于javascript - 如何使用 node.js 和 express 将结果写入文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37013532/

相关文章:

javascript - 如何确定绝对定位元素的容器

mysql - 当我尝试获取上次更新行的 id 时,我得到未定义的信息

javascript - 将应用程序部署到谷歌应用程序引擎并希望启动 api 和客户端

node.js - Express 中间件 protected 和不 protected 路由

javascript - 我有来自 2 个数据库的请求数据。如何将代码合并到一个函数中?

javascript - 在 JavaScript ES6 中,for-in 循环和 for-of 循​​环中,变量 i 在 block 的每次迭代后不再存在?

php - 如何将数据从javascript作为多维数组发送到PHP

javascript - 从文本中提取日期

javascript - Node.js 表达静态图像服务

node.js - Socket.IO:如何删除 namespace