Node和Express应用程序发送POST请求两次

时间:2016-03-16 03:33:01

标签: json node.js express mean-stack mean

这是我的路线代码。

router.post('/content', function(req, res) {
    var topic = req.body.inputText;
    console.log("topic :"+topic); //shows correctly
    var somedata = "";
    console.log("request arrived for URL", req.url); //prints "request arrived for URL /content" only once


    var url = '<the-url-from-which-i-fetch-json'+topic;
    request(url, function (error, response, body) {
      if (!error && response.statusCode == 200) {
        var wikitext = JSON.parse(body);
        console.log("Got a response: ", JSON.stringify(wikitext[0]['content'])); //prints the content only once as needed
         res.render('newcontent', {
            text : JSON.stringify(wikitext[0]['content'])
         });
      } else {
        console.log("Got an error: ", error, ", status code: ", response.statusCode);
      }
    });

});

这是获取的json数据的一个实例。

{"topicname":"wiki","content":"<p>A <strong>wiki</strong> ([audio=en-us-wiki.oggˈwɪki] [WIKee]) is a <a href=\"http://en.wikipedia.org/wiki/website\">website</a> which allows <a href=\"http://en.wikipedia.org/wiki/collaborative_software\">collaborative</a> modification of its content and structure directly from the <a href=\"http://en.wikipedia.org/wiki/web_browser\">web browser</a>. In a typical wiki, text is written using a simplified <a href=\"http://en.wikipedia.org/wiki/markup_language\">markup language</a> (known as \"wiki markup\"), and often edited with the help of a <a href=\"http://en.wikipedia.org/wiki/Online_rich-text_editor\">rich-text editor</a>.(ref: Britannica)<a href=\"http://en.wikipedia.org/wiki/Encyclopædia_Britannica\">Encyclopædia Britannica</a>volume=1publisher=<a href=\"http://en.wikipedia.org/wiki/Encyclopædia_Britannica,_Inc.\">Encyclopædia Britannica, Inc.</a>year=2007location=Londonurl=http://www.britannica.com/EBchecked/topic/1192819/wikiaccessdate=April 10, 2008]</ref></p>\n<p>A wiki is run using <a href=\"http://en.wikipedia.org/wiki/wiki_software\">wiki software</a>, otherwise known as a wiki engine. There are dozens of different wiki engines in use, both standalone and part of other software, such as <a href=\"http://en.wikipedia.org/wiki/bug_tracking_system\">bug tracking system</a>s. Some wiki engines are <a href=\"http://en.wikipedia.org/wiki/open_source\">open source</a>, whereas others are proprietary. Some permit control over different functions (levels of access); for example, editing rights may permit changing, adding or removing material. Others may permit access without enforcing access control. Other rules may also be imposed to organize content. A wiki engine is a type of <a href=\"http://en.wikipedia.org/wiki/content_management_system\">content management system</a>, but it differs from most other such systems, including <a href=\"http://en.wikipedia.org/wiki/blog_software\">blog software</a>, in that the content is created without any defined owner or leader, and wikis have little implicit structure, allowing structure to emerge according to the needs of the users.(ref: Easy Wiki Hosting )</p>\n<p>The encyclopedia project <a href=\"http://en.wikipedia.org/wiki/Wikipedia\">Wikipedia</a> is by far the most popular wiki-based website, and is in fact one of the most widely viewed sites of any kind of the world, having been ranked in the top ten since 2007. Wikipedia is not a single wiki but rather a collection of hundreds of wikis, one for each language. There are at least tens of thousands of other wikis in use, both public and private, including wikis functioning as <a href=\"http://en.wikipedia.org/wiki/knowledge_management\">knowledge management</a> resources, <a href=\"http://en.wikipedia.org/wiki/notetaking_software\">notetaking</a> tools, <a href=\"http://en.wikipedia.org/wiki/Web_community\">community websites</a> and <a href=\"http://en.wikipedia.org/wiki/intranet\">intranet</a>s.</p>\n<p><a href=\"http://en.wikipedia.org/wiki/Ward_Cunningham\">Ward Cunningham</a>, the developer of the first wiki software, <a href=\"http://en.wikipedia.org/wiki/WikiWikiWeb\">WikiWikiWeb</a>, originally described it as \"the simplest online database that could possibly work\". \"<a href=\"http://en.wikipedia.org/wiki/wikt:wiki#Hawaiian\">Wiki</a>\" (pronounced [ˈwiki][group=note/w/lang=haw] <a href=\"http://en.wikipedia.org/wiki/phoneme\">phoneme</a> varies between [wlang=haw] and [vlang=haw], and the realization of the [/k/lang=haw] phoneme varies between [klang=haw] and [tlang=haw], among other realizations. Thus, the pronunciation of the Hawaiian word <em>wiki</em> varies between [wikilang=haw], [witilang=haw], [vikilang=haw], and [vitilang=haw]. See <a href=\"http://en.wikipedia.org/wiki/Hawaiian_phonology\">Hawaiian phonology</a> for more details.}}) is a <a href=\"http://en.wikipedia.org/wiki/Hawaiian_language\">Hawaiian</a> word meaning \"quick\".</p>\n","date":"2016-03-16T03:23:11.735Z"}

这是newcontent.jade

#{text}
p HELLO SSUP

在输出中,我得到了text变量的数据打印两次。我尝试了firefox和chrome,同样的问题仍然存在。 (任何地方都没有favicon加载,因为在某些线程中提到这是一个问题)。那么这段代码可能有什么问题呢?

Output has the text printed twice

在两个内容之后,

HELLO SSUP打印在最后。

1 个答案:

答案 0 :(得分:1)

1)您不需要额外stringify wikitext[0]['content'],因为其类型已经是字符串:

console.log(typeof wikitext[0]['content'])
// string

2)您在模板中使用转义:#{text}

所以:

<强> 路由器

res.render('newcontent', {
   text : wikitext[0]['content']
});

<强> 模板

!{text}
p HELLO SSUP

div!= text
p HELLO SSUP