Skip to content

Commit 630900f

Browse files
committed
Merge pull request http-party#88 from paulmelnikow/corser
Get CORS support working with XHR preflight
2 parents 363d928 + 30f4d1e commit 630900f

File tree

4 files changed

+63
-30
lines changed

4 files changed

+63
-30
lines changed

bin/http-server

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -70,10 +70,7 @@ function listen(port) {
7070
};
7171

7272
if (argv.cors) {
73-
options.headers = {
74-
'Access-Control-Allow-Origin': '*',
75-
'Access-Control-Allow-Headers': 'Origin, X-Requested-With, Content-Type, Accept'
76-
};
73+
options.cors = true;
7774
}
7875

7976
if (ssl) {

lib/http-server.js

Lines changed: 34 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@ var fs = require('fs'),
22
util = require('util'),
33
union = require('union'),
44
ecstatic = require('ecstatic'),
5-
httpProxy = require('http-proxy');
5+
httpProxy = require('http-proxy'),
6+
corser = require('corser');
67

78
var HTTPServer = exports.HTTPServer = function (options) {
89
options = options || {};
@@ -20,9 +21,7 @@ var HTTPServer = exports.HTTPServer = function (options) {
2021
}
2122
}
2223

23-
if (options.headers) {
24-
this.headers = options.headers;
25-
}
24+
this.headers = options.headers || {};
2625

2726
this.cache = options.cache || 3600; // in seconds.
2827
this.showDir = options.showDir !== 'false';
@@ -34,37 +33,47 @@ var HTTPServer = exports.HTTPServer = function (options) {
3433
: options.ext;
3534
}
3635

37-
var serverOptions = {
38-
before: (options.before || []).concat([
39-
function (req, res) {
40-
if (options.logFn) {
41-
options.logFn(req, res);
42-
}
43-
44-
res.emit('next');
45-
},
46-
ecstatic({
47-
root: this.root,
48-
cache: this.cache,
49-
showDir: this.showDir,
50-
autoIndex: this.autoIndex,
51-
defaultExt: this.ext,
52-
handleError: typeof options.proxy !== 'string'
53-
})
54-
]),
55-
headers: this.headers || {}
56-
};
36+
var before = options.before ? options.before.slice() : [];
37+
38+
before.push(function (req, res) {
39+
if (options.logFn) {
40+
options.logFn(req, res);
41+
}
42+
43+
res.emit('next');
44+
});
45+
46+
if (options.cors) {
47+
this.headers['Access-Control-Allow-Origin'] = '*';
48+
this.headers['Access-Control-Allow-Headers'] = 'Origin, X-Requested-With, Content-Type, Accept';
49+
50+
before.push(corser.create());
51+
}
52+
53+
before.push(ecstatic({
54+
root: this.root,
55+
cache: this.cache,
56+
showDir: this.showDir,
57+
autoIndex: this.autoIndex,
58+
defaultExt: this.ext,
59+
handleError: typeof options.proxy !== 'string'
60+
}));
5761

5862
if (typeof options.proxy === 'string') {
5963
var proxy = httpProxy.createProxyServer({});
60-
serverOptions.before.push(function (req, res) {
64+
before.push(function (req, res) {
6165
proxy.web(req, res, {
6266
target: options.proxy,
6367
changeOrigin: true
6468
});
6569
});
6670
}
6771

72+
var serverOptions = {
73+
before: before,
74+
headers: this.headers
75+
};
76+
6877
if (options.https) {
6978
serverOptions.https = options.https;
7079
}

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,8 @@
6464
"ecstatic": "~0.6.1",
6565
"http-proxy": "^1.8.1",
6666
"portfinder": "0.2.x",
67-
"opener": "~1.4.0"
67+
"opener": "~1.4.0",
68+
"corser": "~2.0.0"
6869
},
6970
"devDependencies": {
7071
"vows": "0.7.x",

test/http-server-test.js

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,5 +114,31 @@ vows.describe('http-server').addBatch({
114114
}
115115
}
116116
}
117+
},
118+
'When cors is enabled': {
119+
topic: function () {
120+
var server = httpServer.createServer({
121+
root: root,
122+
cors: true
123+
});
124+
server.listen(8082);
125+
this.callback(null, server);
126+
},
127+
'and given OPTIONS request': {
128+
topic: function () {
129+
request({
130+
method: 'OPTIONS',
131+
uri: 'http://127.0.0.1:8082/',
132+
headers: {
133+
'Access-Control-Request-Method': 'GET',
134+
Origin: 'http://example.com',
135+
'Access-Control-Request-Headers': 'Foobar'
136+
}
137+
}, this.callback);
138+
},
139+
'status code should be 204': function (err, res, body) {
140+
assert.equal(res.statusCode, 204);
141+
}
142+
}
117143
}
118144
}).export(module);

0 commit comments

Comments
 (0)