Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Avatar for KuCalc : devops.
Download
50665 views
1
/*
2
* http.js: Top-level plugin exposing HTTP features in flatiron
3
*
4
* (C) 2011, Nodejitsu Inc.
5
* MIT LICENSE
6
*
7
*/
8
9
var director = require('director'),
10
flatiron = require('../../flatiron'),
11
union;
12
13
try {
14
//
15
// Attempt to require union.
16
//
17
union = require('union');
18
}
19
catch (ex) {
20
//
21
// Do nothing since this is a progressive enhancement
22
//
23
console.warn('flatiron.plugins.http requires the `union` module from npm');
24
console.warn('install using `npm install union`.');
25
console.trace();
26
process.exit(1);
27
}
28
29
//
30
// Name this plugin.
31
//
32
exports.name = 'http';
33
34
exports.attach = function (options) {
35
var app = this;
36
37
//
38
// Define the `http` namespace on the app for later use
39
//
40
app.http = app.http || {};
41
42
app.http = flatiron.common.mixin({}, app.http, options || {});
43
44
app.http.before = app.http.before || [];
45
app.http.after = app.http.after || [];
46
app.http.headers = app.http.headers || {
47
'x-powered-by': 'flatiron ' + flatiron.version
48
};
49
50
app.router = new director.http.Router().configure({
51
async: true
52
});
53
54
app.start = function (port, host, callback) {
55
if (!callback && typeof host === 'function') {
56
callback = host;
57
host = null;
58
}
59
60
app.init(function (err) {
61
if (err) {
62
if (callback) {
63
return callback(err);
64
}
65
66
throw err;
67
}
68
69
app.listen(port, host, callback);
70
});
71
};
72
73
app.createServer = function(){
74
app.server = union.createServer({
75
after: app.http.after,
76
before: app.http.before.concat(function (req, res) {
77
if (!app.router.dispatch(req, res, app.http.onError || union.errorHandler)) {
78
if (!app.http.onError) res.emit('next');
79
}
80
}),
81
headers: app.http.headers,
82
limit: app.http.limit,
83
https: app.http.https
84
});
85
};
86
87
app.listen = function (port, host, callback) {
88
if (!callback && typeof host === 'function') {
89
callback = host;
90
host = null;
91
}
92
93
app.createServer();
94
95
return host
96
? app.server.listen(port, host, callback)
97
: app.server.listen(port, callback);
98
};
99
};
100
101