helloworld2.js 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. /* a more advanced hello world plugin... it listens for "POST /hello.cs", "POST /hello.as" and "POST /hello" requests... "POST /hello.cs" requests are received and handled by "csworker"... "POST /hello.as" requests are received by "csworker" and forwarded to "asworker" for handling... "POST /hello" requests trigger a misuse error */
  2. const {
  3. __errorContinuable,
  4. __httpReq,
  5. __wHttpReq,
  6. __postInternalErrorMsg, __postMessage,
  7. __sequenceNumber,
  8. __successContinuable,
  9. __uri_to_id
  10. } = require("../__worker");
  11. const _do = require("../___do");
  12. module.exports = {
  13. 'interfaces' :
  14. [{'method':'POST', 'urlm':'^/hello\.[ac]s$'},
  15. {'method':'POST', 'url=':'/hello'}],
  16. 'localcontext' : {'counter':0},
  17. 'csworker' :
  18. function(resp,method,uri,reqData,wcontext)
  19. {
  20. if( uri.match(/\/hello.cs$/) )
  21. {
  22. this.localcontext.counter++;
  23. __postMessage(
  24. {'statusCode':200,
  25. 'changelog':
  26. [{'op':'SYSOUT',
  27. 'text':'Hello #'+this.localcontext.counter+' from csworker #'+__wid}],
  28. 'sequence#':__sequenceNumber(),
  29. 'respIndex':resp});
  30. }
  31. else if( uri.match(/\/hello.as$/) )
  32. _do.chain(
  33. [__wHttpReq(method, uri+'?wid='+wcontext.__aswid, reqData)])(
  34. function()
  35. {
  36. __postMessage(
  37. {'statusCode':200,
  38. 'respIndex':resp});
  39. },
  40. function(err) {__postInternalErrorMsg(resp,err);}
  41. );
  42. else if( uri.match(/\/hello$/) )
  43. __postInternalErrorMsg(resp,'must request /hello.cs or /hello.as');
  44. },
  45. 'asworker' :
  46. function(resp,method,uri,reqData,wcontext)
  47. {
  48. this.localcontext.counter++;
  49. __postMessage(
  50. {'statusCode':200,
  51. 'changelog':
  52. [{'op':'SYSOUT',
  53. 'text':'Hello #'+this.localcontext.counter+' from asworker #'+__wid}],
  54. 'sequence#':__sequenceNumber(),
  55. 'respIndex':resp});
  56. }
  57. };