私は、elasticsearchの上にnode.jsとexpress.jsを使用してアプリケーションを構築しました。これは、検索ボックスを備えた非常に簡単なアプリケーションです。クエリを検索すると、結果がJSON形式で出力されます。私は、キーワードを検索した場合などについては、「白」は、出力は次のようになります。node.jsを使って結果をファイルに書き込む方法を教えてください。
: http://i.stack.imgur.com/VHuWl.pngは、今私は(例えばoutput.json用)ファイルで、この結果を保存したい.Thisは私の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を使って出力をファイルに書き出す方法はありますか?
編集:私は
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を編集した。しかし、私はこのJavaScriptを実行したときに、私はこの出力を得た: http://i.stack.imgur.com/rfBq0.png
私はJSON出力を得ていないのです。私の出力は次のようになります。http://i.stack.imgur.com/VHuWl.png
私もこれに似ています(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>
私はこのからの出力を取得する必要がありますファイル?私はこのブロックで探しています
私の更新された投稿を確認してください。私はあなたの第1のソリューションを使用しようとしましたが、今は私の "output.json"はこのhttp://i.stack.imgur.com/rfBq0のように見えます。png – Rose
これはすでに優れています。あなたのデータに依存しますが、JSON.stringify(data)のようなものを使用して、データをプレーンなJSONに変換する必要があります。 – TGrif
フロントエンドに結果を表示するためのejsファイルも使用しています。私はそのファイルを含めるために自分の投稿を更新しました。 JSON.stringifyをここで使用したので、このejsファイルから結果を取得する必要がありますか? – Rose