Use Nodejs to get blog Park blog data and handle forwarding

Source: Internet
Author: User

Through the query to understand that the blog Park has developed a blog query related interfaces, the list is as follows:

Uri Method Description
48hourstopviewposts/{itemcount} GET 48-hour Reading ranking
Bloggers/recommend/{pageindex}/{pagesize} GET Page to get a list of recommended blogs
Bloggers/recommend/count GET Get the total number of recommended blogs
Bloggers/search GET Search Bloggers by author name
Post/{postid}/comments/{pageindex}/{pagesize} GET Get article comments
Post/body/{postid} GET Get article content
Sitehome/paged/{pageindex}/{pagesize} GET Pagination Get home page article List
Sitehome/recent/{itemcount} GET Get home page article List
Tendaystopdiggposts/{itemcount} GET Recommended rankings within 10 days
U/{blogapp}/posts/{pageindex}/{pagesize} GET Page to get a list of personal blog posts

But when we open one of these interfaces, we find that the provided interface returns XML-formatted content, so you need to convert the XML to JSON data if you need a format that needs to be forwarded back to the foreground:

Then we step by step, first need node to forward this interface proxy to its own interface, in fact, only need express request middleware can be:

Router.get ('/api/getcnblog ', (req, res) and {let    url = ' HTTP://WCF.OPEN.CNBLOGS.COM/BLOG/TENDAYSTOPDIGGPOSTS/6 ';    Let data = {}    request (URL, (error, response, body) = = {            res.send (body)})    })

But this time the interface returns data that is still in XML format. So the next step, we need to transform the XML, through the search to understand that node has XML2JS this plug-in, so just need to be introduced in our node file, and then transform the data. The simple code after the change is as follows:

Const XML2JS = require (' XML2JS '), const parsestring = xml2js.parsestring;router.get ('/api/getcnblog ', (req, res) = = { C0/>let url = ' HTTP://WCF.OPEN.CNBLOGS.COM/BLOG/TENDAYSTOPDIGGPOSTS/6 ';    Let data = {}    request (URL, (error, response, body) = {        parsestring (body, (err, result) = {            Res.send ({ c5/>code:200,                data:result            })        })    

As a result, you can see that the data sent back to the background has been converted to JSON format:

Done ~

Use Nodejs to get blog Park blog data and handle forwarding

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.