github.com/craicoverflow/tyk@v2.9.6-rc3+incompatible/middleware/sampleMiddleware.js (about)

     1  // ---- Sample middleware creation by end-user -----
     2  var sampleMiddleware = new TykJS.TykMiddleware.NewMiddleware({});
     3  
     4  sampleMiddleware.NewProcessRequest(function(request, session) {
     5      // You can log to Tyk console output by calloing the built-in log() function:
     6      log("Running sample JSVM middleware")
     7      
     8      // Set and Delete headers in an outbound request
     9      request.SetHeaders["User-Agent"] = "Tyk-Custom-JSVM-Middleware";
    10      //request.DeleteHeaders.push("Authorization");
    11      
    12      // Change the outbound URL Path (only fragment, domain is fixed)
    13      // request.URL = "/get";
    14      
    15      // Add or delete request parmeters, these are encoded for the request as needed.
    16      request.AddParams["test_param"] = "My Teapot";
    17      request.DeleteParams.push("delete_me");
    18      
    19      // Override the body:
    20      request.Body = "New Request body"
    21      
    22      // If you have multiple middlewares that need to communicate, set or read keys in the session object.
    23      // This will only work in a postprocessing MW
    24      if (session.meta_data) {
    25          session.meta_data["MiddlewareDataString"] = "SomeValue";
    26      }
    27         
    28      // You MUST return both the request and session metadata    
    29      return sampleMiddleware.ReturnData(request, session.meta_data);
    30  });
    31  
    32  // Ensure init with a post-declaration log message
    33  log("Sample middleware initialised");