Mikael's blog

A developers sixth time trying to maintain a blog

Source Code - Root / responseManager.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
/*****************************************
*   Response Manager
*****************************************
*   Author:  mikael.lofjard@gmail.com
*   Website: http://lofjard.se
*   License: MIT License
****************************************/
 
var ResponseManager = (function () {
 
  var env = require('./environmentManager').EnvironmentManager;
  var cm = require('./cacheManager').CacheManager;
 
  var misc = require('./misc').Misc;
 
  return {
 
    writeCachedResponse : function (response, cachedUrl) {
      env.info('ResponseManager: Writing cached view for ' + cachedUrl);
 
        var data = cm.fetch(cachedUrl);
 
        response.writeHead(200, data.headers);
        response.write(data.content, 'utf-8');
        response.end();
        return;
    },
 
    writeResponse : function (request, response, responseData, doNotCache) {
      var pathName = misc.getPathName(request.url);
 
      if (typeof(doNotCache) == 'undefined') {
        cm.cache(pathName, responseData.headers, responseData.content)
      }
 
      response.writeHead(200, responseData.headers);
      response.write(responseData.content, 'utf-8');
      response.end();
      return;
    }
  };
 
}());
 
typeof(exports) != 'undefined' ? exports.ResponseManager = ResponseManager : null;