Node synchronizes JS Code-surpassing yesterday's series (5)

Source: Internet
Author: User
Tags emit
Surpassing yesterday's series (5) If you do not know what node. JS is, Google is recommended. Even if you can guarantee that your project will not be used in the future, you cannot guarantee that your future investment will not be involved.

For node Article : Touch my node. js through asynchronous mechanism so that the request is not blocked, to achieve the purpose of parallel requests, this is also it can become a server Code Because of the absence of blocking, it has a high capacity to accept requests, but in fact, if the business implementation still uses it, it will inevitably drag down its performance. For example, it is best to separate time-consuming work from its receiving request module asynchronously. In actual encoding, the sequence execution problem will occur: Step 1: retrieve data from database a table A Step 2: to write data from Table A to table B, you must obtain data from Table A before writing data to table B. First, you must write two functions and call them one by one. This will lead to the pressure on a single-threaded node to process operations like databases, because it will not be able to process requests during this time !!! Solution 1: Use the callback function (note the annotation)
Exports. News = Function  (Req, Res) {logger.info ( 'Request for/News' );  VaR Info = Req. Body;  VaR Userid = Null  ; VaR Advids = [];  //  Database processing step 1 Dao. getuseridandareabyimsiandappid (info. imsi, info. appid, Function  (ERR, rows ){  If  (ERR) {logger. Error ( 'News error' );} Logger. debug ( "News: % J" , Info );  If (Rows = Null | Rows. Length = 0) {Res. writehead ( 200, {"Content-Type": "application/JSON", 'charset': 'utf-8' }); Res. End ();  Return  ;} Userid = Rows [0 ]. ID;  VaR Areacode = rows [0 ]. Area;  //  Step 2 Dao. getadvids (userid, info. appid, Function  (ERR, rows ){  If (ERR) {res. writehead ( 200, {"Content-Type": "application/JSON", 'charset': 'utf-8' }); Res. End (); logger. Error (ERR );  Return  ;}  If (Rows. Length <= 0 ) {Res. writehead ( 200, {"Content-Type": "application/JSON", 'charset': 'utf-8' }); Res. End ();  Return  ;}  For (VaR Index In  Rows) {advids. Push (rows [Index]. adv_id );}  //  Database processing step 3 Dao. getadv (info. adstyle, areacode, advids, Function  (ERR, rows ){  If  (ERR) {res. writehead ( 200, {"Content-Type": "application/JSON", 'charset': 'utf-8' }); Logger. Error ( 'Getadv error: '+Err); res. End ();  Return  ;}  If (Rows. Length <= 0 ) {Logger. debug ( 'No ads' ); Res. writehead ( 200, {"Content-Type": "application/JSON", 'charset': 'utf-8' }); Res. End ();  Return  ;}  VaR Advert = rows [0];  //  Step 4 Dao. updateadvpush_state (userid, info. appid, advert. ID, Function  (ERR, RST ){  If  (ERR) {logger. Error ( 'Update advert in tb_user_advert by advid error "id = '+ advert. ID +', '+ Err );  //  More error handling action                          Return ;} Logger. debug ( 'Update advert push state success: advid = % d' , Advert. ID) ;}); res. writehead ( 200, {"Content-Type": "application/JSON" }); Res. Write (advert. Body); res. End ();});});});}; 
Solution 2: Use node event listening
Exports. Location = Function (Req, Res) {logger.info ( "-- Location begin --" );  VaR Info = Req. Body;  VaR Imsi = Info. imsi;  VaR Appid = Info. appid;  VaR Lat = Info. Lat;  VaR LGT = Info. LGT; logger.info ( "Imsi:" + imsi + "appid:" + appid + "Lat:" + lat + "LGT:" +LGT );  VaR  City;  VaR  Area;  //  Database processing step 1 Getcity (Lat, LGT, Function  (ERR, RST) {City = RST. City;  If (! City) {logger. debug ( 'City not exist: LGT = % s, lat = % s' , LGT, Lat);} logger. debug ( 'City got: LGT = % s, lat = % s', LGT, Lat); Dao. emit ( 'Getcityok' );});  //  Step 2 Dao. Once ('getcityok ', Function  () {Dao. getcitycode (city,  Function  (ERR, rows) {logger. debug ( 'Get into taggetcitycode' );  If  (ERR) {logger. Error ( 'Getcitycode error, city = % s' , City );} If (Rows. Length <= 0 ) {Logger. debug ( 'City not exist in the Databse, city: % s' , City); User. Area = Undefined ;}  Else  {Area = Rows [0 ]. ID; logger. debug ( 'Get city code: Id = % d' , Area);} Dao. emit ( 'Getcitycodeok' );});});  //  Database processing step 3 Dao. Once ('getcitycodeok ', Function  () {Dao. updateuserarea (imsi, appid, area,  Function  (ERR, RST ){  If  (ERR) {logger. Error ( 'Update area in tb_user by imsi and app_id error "imsi = '+ imsi +', '+" app_id = "+ appid + Err );  Return  ;} Logger. debug ( 'Update user area success: Area = % s' , Area) ;}}); logger.info ( "-- Location end --"); Res. Send ( '' );}; 

 

Solution N: No matter which method is used for the above code, the code is very variable, difficult to read, and difficult to reuse the code. There is a solution written by Niu Ren himself: Async: Https://github.com/caolan/async Eventproxy: Https://github.com/JacksonTian/eventproxy Jscex: Https://github.com/JeffreyZhao/jscex

 

 

--------------------------------------------------

Let's move on!

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.