title: Calling same request again in node express
body: <p>I am using Express to proxy requests to a separate API server which is protected by OAuth 2 access tokens. When a token expires, the server will return a 401 which I am now handling in my router middleware to then go and refresh the access token associated with the clients session (I am using <code>express-session</code>).</p>
  
  <p>Here is my code:</p>
  
  <p><strong>router.js</strong></p>
  
  <p><code>app.use('/api', require('./routes.js'));</code></p>
  
  <p><strong>routes.js</strong></p>
  
  <pre><code>var express = require('express'),
    router = express.Router(),
    routesController = require('./routes.controller.js');
  
  router.route('/*')
    .get(routesController.fetch);
  </code></pre>
  
  <p><strong>routes.controller.js</strong></p>
  
  <pre><code>module.exports.fetch = function(req, res, next) {
    var options = helpers.buildAPIRequestOptions(req);
    request(options, function(err, response, body){
      if(response.statusCode === 401) {
        authController.refreshToken(req, res, next);
      } else {
        res.status(response.statusCode).send(body);
      }
    });
  };
  </code></pre>
  
  <p><strong>authController</strong></p>
  
  <pre><code>module.exports.refreshToken = function(req, res, next) {
    var formData = {
        grant_type: 'refresh_token',
        refresh_token: req.session.refreshToken,
        scope: 'PRODUCTION'
      },
      headers = {
        'Authorization' : 'Basic ' + consts.CLIENT_KEY_SECRET_BASE64_DEV
      };
    request.post({url:consts.ACCESS_TOKEN_REQUEST_URL_DEV, form:formData, headers: headers, rejectUnauthorized: false}, function(err, response, body){
      var responseBody = JSON.parse(body);
      if (response.statusCode === 200) {
  
        req.session.accessToken = responseBody.access_token;
        req.session.refreshToken = responseBody.refresh_token;
        next();
        //How to recall the original request made from fetch controller function after this point?
      } else {
        console.log('SOMETHING ELSE HAPPENED!');
      }
    });
  };
  </code></pre>
  
  <p>After updating the token, I would like to re-issue the original API request that I am triggering using the <code>request</code> module in my fetch controller.</p>
  
  <p>I'm a bit stumped how I actually go about doing this, is there an elegant way to achieve this?</p>