我使用node.js和express.js构建了一个基于elasticsearch的应用程序。这是一个非常简单的应用程序,其中包含一个搜索框。当您搜索一个查询时,它会以JSON格式输出结果。例如,如果我搜索关键词“white”,输出如下所示:http://i.stack.imgur.com/VHuWl.png
现在,我想将这个结果存储在一个文件中(例如output.json)。这是我的json文件:
当我尝试使用node.js的writeFile方法时,出现了引用错误,显示"data"未定义。我的错误看起来像这样:http://i.stack.imgur.com/lwXfW.png 我无法弄清楚这个错误。是否有其他方法可以使用node.js和express将输出写入文件?
注:我已编辑了我的JavaScript代码。
当我运行这段 JavaScript 时,得到了如下输出: http://i.stack.imgur.com/rfBq0.png 我没有得到 JSON 输出,我的输出应该像这样: http://i.stack.imgur.com/VHuWl.png 我还在前端使用带有 JavaScript 的 ejs 文件(index.ejs),它看起来像这样:
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方法时,出现了引用错误,显示"data"未定义。我的错误看起来像这样: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
在您的post方法中是有效的,所以您可以选择1.从post方法中调用writeFile
,或者2.在文件顶部声明一个新变量来存储在post方法中传入的data
。 - Andy HoffmanJSON.stringify(data)
。 - Andy Hoffmandata
,并在.ejs文件中尝试使用<%- JSON.stringify(data) %>
。 - Andy Hoffman