このプロジェクトにはwikipedia-jsを使用しました。summary.js
これはファイルの私のコードです。
var wikipedia = require("wikipedia-js");
var something = "initial";
module.exports = {
wikitext: function(topicname) {
console.log("Inside wikitex funciton :" + topicname);
var options = {
query: topicname,
format: "html",
summaryOnly: false,
lang: "en"
};
wikipedia.searchArticle(options, function(err, htmlWikiText) {
console.log("Inside seararticlefunciton :");
if (err) {
console.log("An error occurred[query=%s, error=%s]", topicname, err);
return;
}
console.log("Query successful[query=%s, html-formatted-wiki-text=%s]", topicname, htmlWikiText);
something = htmlWikiText;
});
return something;
},
};
このモジュールは/wiki/:topicname
ルートで使用しています。での対応コードindex.js
はこんな感じ。
router.get('/wiki/:topicname', function(req, res, next) {
var topicname = req.params.topicname;
console.log(topicname);
var first = summary.wikitext(topicname);
res.send("Hello "+first);
});
問題は、wiki/ some-topicsummary.js
にアクセスするたびに、 executesの最後の return ステートメントにhtmlWikiText
コンテンツが入力されることです。だから私はいつもhello initial
ブラウザのページに表示されます。console.log
しばらくすると、ステートメントのために端末に出力されますが。
では、この問題をどのように解決すればよいでしょうか。