socketioMEANseed/www/node_modules/http-server/lib/http-server.js
www/node_modules/http-server/lib/http-server.jsRaw
1'use strict';
2
3var fs = require('fs'),
4 union = require('union'),
5 ecstatic = require('ecstatic'),
6 httpProxy = require('http-proxy'),
7 corser = require('corser');
8
9//
10// Remark: backwards compatibility for previous
11// case convention of HTTP
12//
13exports.HttpServer = exports.HTTPServer = HttpServer;
14
15/**
16 * Returns a new instance of HttpServer with the
17 * specified `options`.
18 */
19exports.createServer = function (options) {
20 return new HttpServer(options);
21};
22
23/**
24 * Constructor function for the HttpServer object
25 * which is responsible for serving static files along
26 * with other HTTP-related features.
27 */
28function HttpServer(options) {
29 options = options || {};
30
31 if (options.root) {
32 this.root = options.root;
33 }
34 else {
35 try {
36 fs.lstatSync('./public');
37 this.root = './public';
38 }
39 catch (err) {
40 this.root = './';
41 }
42 }
43
44 this.headers = options.headers || {};
45
46 this.cache = options.cache === undefined ? 3600 : options.cache; // in seconds.
47 this.showDir = options.showDir !== 'false';
48 this.autoIndex = options.autoIndex !== 'false';
49 this.contentType = options.contentType || 'application/octet-stream';
50
51 if (options.ext) {
52 this.ext = options.ext === true
53 ? 'html'
54 : options.ext;
55 }
56
57 var before = options.before ? options.before.slice() : [];
58
59 before.push(function (req, res) {
60 if (options.logFn) {
61 options.logFn(req, res);
62 }
63
64 res.emit('next');
65 });
66
67 if (options.cors) {
68 this.headers['Access-Control-Allow-Origin'] = '*';
69 this.headers['Access-Control-Allow-Headers'] = 'Origin, X-Requested-With, Content-Type, Accept, Range';
70 if (options.corsHeaders) {
71 options.corsHeaders.split(/\s*,\s*/)
72 .forEach(function (h) { this.headers['Access-Control-Allow-Headers'] += ', ' + h; }, this);
73 }
74 before.push(corser.create(options.corsHeaders ? {
75 requestHeaders: this.headers['Access-Control-Allow-Headers'].split(/\s*,\s*/)
76 } : null));
77 }
78
79 if (options.robots) {
80 before.push(function (req, res) {
81 if (req.url === '/robots.txt') {
82 res.setHeader('Content-Type', 'text/plain');
83 var robots = options.robots === true
84 ? 'User-agent: *\nDisallow: /'
85 : options.robots.replace(/\\n/, '\n');
86
87 return res.end(robots);
88 }
89
90 res.emit('next');
91 });
92 }
93
94 before.push(ecstatic({
95 root: this.root,
96 cache: this.cache,
97 showDir: this.showDir,
98 autoIndex: this.autoIndex,
99 defaultExt: this.ext,
100 contentType: this.contentType,
101 handleError: typeof options.proxy !== 'string'
102 }));
103
104 if (typeof options.proxy === 'string') {
105 var proxy = httpProxy.createProxyServer({});
106 before.push(function (req, res) {
107 proxy.web(req, res, {
108 target: options.proxy,
109 changeOrigin: true
110 });
111 });
112 }
113
114 var serverOptions = {
115 before: before,
116 headers: this.headers,
117 onError: function (err, req, res) {
118 if (options.logFn) {
119 options.logFn(req, res, err);
120 }
121
122 res.end();
123 }
124 };
125
126 if (options.https) {
127 serverOptions.https = options.https;
128 }
129
130 this.server = union.createServer(serverOptions);
131}
132
133HttpServer.prototype.listen = function () {
134 this.server.listen.apply(this.server, arguments);
135};
136
137HttpServer.prototype.close = function () {
138 return this.server.close();
139};