【问题标题】:Node and Express application sending POST request twiceNode 和 Express 应用程序发送两次 POST 请求
【发布时间】:2016-07-01 18:44:10
【问题描述】:

这是我的路线代码。

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 打印在最后,在两个内容之后。

【问题讨论】:

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


    【解决方案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
    

    【讨论】:

    • 如何将其转义打印两次?
    • 这很可能是一个错误:模板试图将输出变量的内容包装在标签中,但无法应对。以p #{text} 为例。
    猜你喜欢
    • 2018-02-09
    • 1970-01-01
    • 2022-10-12
    • 1970-01-01
    • 1970-01-01
    • 2017-11-17
    • 1970-01-01
    • 2020-06-09
    • 1970-01-01
    相关资源
    最近更新 更多