Mikael's blog

A developers sixth time trying to maintain a blog

Source Code - Root / server.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
/*****************************************
 *   Server
 *****************************************
 *   Author:  mikael.lofjard@gmail.com
 *   Website: http://lofjard.se
 *   License: MIT License
 ****************************************/
  
var Server = (function () {
  
  var http = require('http');
  
  var options = {};
  var httpPort = 8080;
  
  return {
  
    init: function (serverConfig) {
      httpPort = serverConfig.httpPort || httpPort;
    },
  
      start: function (routerCallback) {
  
        function onRequest(request, response) {
          if (typeof(request.headers['host']) != 'undefined') {
            routerCallback(request, response);
          } else {
            // Give 400 Bad Request if no Host header field exists
            response.writeHead(400);
            response.end();
            return;
          }
        }
  
        http.createServer(onRequest).listen(httpPort);
        console.log('Server: Running at http://localhost:' + httpPort);
      }
  };
  
}());
  
typeof(exports) != 'undefined' ? exports.Server = Server : null;