Node. JS enables serialization of process control

Source: Internet
Author: User

To demonstrate how to implement serial process control, we are going to make a small program that gets a title and URL from a randomly selected RSS feed and displays it.

The RSS feed list is placed in the Rss_feeds.txt file with the following content:

Http://feed.cnblogs.com/blog/u/376823/rsshttp://lambda-the-ultimate.org/rss.xml

We need to install two modules before running the program: The request module is a simplified HTTP client that you can use to get RSS data. The Htmlparser module converts the original RSS data into a JavaScript structure.

Use the following command:

NPM Install REQUESTNPM Install Htmlparser

The code is as follows:

varFS = require (' FS ');varRequest = require (' request '));varHtmlparser = require (' Htmlparser '));varConfigfilename = './rss_feeds.txt ';functionCheckforrssfile () {fs.exists (Configfilename,function(exists) {if(!exists)returnNextNewError (' Missing RSS file: ' +configfilename)); Next (NULL, Configfilename); });}functionReadrssfile (configfilename) {fs.readfile (Configfilename,function(Err, feedlist) {if(ERR)returnNext (ERR); Feedlist=feedlist. toString (). Replace (/~\s+|\s+$/g, "). Split ("\ n"); varRandom = Math.floor (Math.random () *feedlist.length); Next (NULL, Feedlist[random]); });}functionDownloadrssfeed (Feedurl) {request ({Uri:feedurl},function(err, res, body) {if(ERR)returnNext (ERR); if(Res.statuscode! = 200)            returnNextNewError (' Abnormal response status code ')); Next (NULL, body); });}functionParserssfeed (RSS) {varHandler =NewHtmlparser.    RssHandler (); varParser =NewHtmlparser.    Parser (handler);    Parser.parsecomplete (RSS); if(!handler.dom.items.length)returnNextNewError (' No RSS items found ')); varitem =Handler.dom.items.shift ();    Console.log (Item.title); Console.log (Item.link);}vartasks =[Checkforrssfile, Readrssfile, Downloadrssfeed, parserssfeed];functionNext (err, result) {if(ERR)Throwerr; varCurrenttask =Tasks.shift (); if(currenttask) {currenttask (result); }}next ();

Node. JS enables serialization of process control

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.