如何使用node.js和express在文件中写入结果
How to write result in a file by using node.js and express?
我在elasticsearch之上使用node.js和express.js构建了一个应用程序。这是一个非常简单的带有搜索框的应用程序。当您搜索查询时,它会以JSON格式打印结果。例如,如果我搜索关键字"white",输出如下所示:https://i.stack.imgur.com/VHuWl.png
现在我想把这个结果存储在一个文件中(例如output.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方法时,我得到了一个Reference错误,显示没有定义"data"。我的错误如下:https://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时,我得到了以下输出:https://i.stack.imgur.com/rfBq0.png
我没有得到JSON输出。我的输出应该是这样的:https://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);
})
我正在查看这个块:
fs.writeFile(path,data,function(err){
if(err) console.error(err);
})
您已经在代码的顶部声明了path
,但在此上下文中data
似乎是未定义的。
- webpack代码拆分了handlerbs文件——结果是文件很大
- 在IntelliJ IDEA中,如何将JS文件从“;查找用法“;使用TypeScript时的结果
- 使用blob从ajax结果下载文件
- 一个javascript实现base64图像编码并将结果写入文本文件
- 在页面对象文件中验证sendKeys结果会导致未定义的错误(Protractor)
- 如何使用node.js和express在文件中写入结果
- 搜索XML文件并使用javascript显示结果
- 如何将 JavaScript 中 HTML 的结果写入文件
- 如何将结果从 Javascript 导出到 csv 文件
- 迭代 JSON 文件并且未在 Javascript 中正确返回结果
- HTML5 文件阅读器如何返回结果
- NodeJS中的Parse Json文件在Node中显示意外结果
- 读取一个本地文件,编码为base64,我想给用户一个将结果保存到文件的选项
- jquery我应该如何在多个html文件中实时搜索结果
- 如何在javascript变量中获取php文件的结果
- 使用AJAX上传文件、处理并使用Flask将结果返回到Javascript
- 从PHP文件结果自动刷新标题网页
- 处理ASP.. NET MVC文件结果返回
- 如何使用javascript读取iqy文件结果表
- ASP.NET MVC 返回文件结果,其中包含要在客户端处理的其他数据