Skip to content

Commit b51e9e6

Browse files
authored
Merge pull request #12 from wearereasonablepeople/renan/cors
Add cors to files proxy
2 parents a235235 + f4c7e83 commit b51e9e6

File tree

2 files changed

+14
-1
lines changed

2 files changed

+14
-1
lines changed

lib/proxy-files.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,22 @@ const pump = require('pump');
66

77
const {paintStatus, log, withTimer, immediately} = require('./utils');
88

9+
const headers = {
10+
'Access-Control-Allow-Origin': '*',
11+
'Access-Control-Allow-Methods': 'OPTIONS, POST, GET, PUT, DELETE',
12+
};
13+
914
const run = ({port, directory, fileExt = 'json', keepExtensions = true, slowMode = false}) => {
1015
const invoke = slowMode ? withTimer : immediately;
1116
const filesProxy = http.createServer();
1217

1318
const files = (req, res) => {
19+
if (req.method === 'OPTIONS') {
20+
res.writeHead(204, headers);
21+
res.end();
22+
return;
23+
}
24+
1425
const urlFile = req.url.split('/').splice(-1).pop() || `index.${fileExt}`;
1526
const urlFileExt = urlFile.indexOf('.') > -1 ? urlFile.split('.').splice(-1).pop() : null;
1627
const urlFileName = urlFile.split(`.${urlFileExt}`)[0];
@@ -24,6 +35,8 @@ const run = ({port, directory, fileExt = 'json', keepExtensions = true, slowMode
2435
fs.exists(filePath, exist => {
2536
const status = exist ? 200 : 404;
2637
const paintedStatus = paintStatus(status);
38+
res.writeHead(status, headers);
39+
2740
if (exist) {
2841
const stream = fs.createReadStream(filePath);
2942
invoke(() => pump(stream, res, log(`${paintedStatus} ${chalk.bold(method)} ${filePath}`)));

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "warp-proxy",
3-
"version": "1.3.5",
3+
"version": "1.4.0",
44
"description": "Proxy requests or return mocks from local files instead",
55
"main": "index.js",
66
"scripts": {

0 commit comments

Comments
 (0)