title: How to intercept third party http request in NodeJS?
body: <p>I need to call a third party API server in NodeJS, as you can see below, I need to use <code>req</code> and <code>resp</code> inside <code>request()</code> method.</p>
  
  <p>Since there are a lot of APIs that I need to use, I don't want to <code>set headers</code> and <code>set response cookies</code> every time I use <code>request()</code>.</p>
  
  <p>Is there anything I can do to intercept <code>request()</code>?</p>
  
  <pre><code>router.post('/register', function (req, resp) {
      var api = "/user/register"
      var data = req.body)
      request({
          url: api,
          method: "POST",
          json: true,
          headers: {'session_id' : req.cookies.session_id},
          body: {
              "tel": data.tel, "code": data.code,
              "password": data.passwd, "referee": data.recommend
          }
      }, function (error, response, body) {
          resp.cookies('session_id', response.headers['sessionId'])
          resp.json(body)
      })
  })
  </code></pre>