2016-03-16 83 views
0

这是我的路由代码。节点和Express应用程序两次发送POST请求

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可变的印刷twice.I试图在两个Firefox和Chrome中的数据,而同样的问题仍然存在。 (没有favicon在任何地方加载,因为这被认为是某些线程中的问题)。那么这段代码可能会出错?

Output has the text printed twice

HELLO SSUP被印刷在端部,这两个内容之后。

回答

1

1)你不需要额外stringifywikitext[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 
+0

是如何逃脱它打印两次? –

+0

这很可能是一个错误:模板试图在标签中包装输出变量的内容,并且无法应付这种情况。例如尝试'p#{text}'。 –

+0

这也工作..!谢谢。 –