Skip to content

Commit a87680c

Browse files
committed
chore(proxy): add cors proxy server to npm registry server
1 parent 1f2ec3c commit a87680c

File tree

5 files changed

+247
-5
lines changed

5 files changed

+247
-5
lines changed

.env

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
1-
REACT_APP_NPM_REGISTRY_API_BASE_URL=http://localhost:8000/npm-registry
1+
REACT_APP_NPM_REGISTRY_API_BASE_URL=http://localhost:8000/https://registry.npmjs.org
22
REACT_APP_NPM_REGISTRY_API_CACHE_ENABLED=true

NOTES.md

+19-2
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,14 @@ Bellow, you will find some notes I took along the way.
55
## Table of Contents
66

77
* `create-react-app related`
8-
* [Error npm test on MacOs Sierra](error-npm-test-on-macos-sierra)
8+
* [Error npm test on MacOs Sierra](#error-npm-test-on-macos-sierra)
9+
* [React specific](#react-specific)
10+
* [Miscellaneous](#miscellaneous)
11+
* [CORS anywhere development proxy](#cors-anywhere-development-proxy)
912

10-
## Error npm test on MacOs Sierra
13+
## create-react-app related
14+
15+
### Error npm test on MacOs Sierra
1116

1217
Due to the following error:
1318

@@ -28,3 +33,15 @@ npm ERR! Test failed. See above for more details.
2833
I installed [watchman](https://facebook.github.io/watchman/docs/install.html), which solved the problem ([known problem on MacOS](https://github.com/facebook/create-react-app/issues/871)).
2934

3035
Note: `create-react-app` doesn't rely on the latest version `jest` (currently `[email protected]`). This [should be resolved](https://github.com/amasad/sane/pull/91) for more recent versions.
36+
37+
## React specific
38+
39+
## Miscellaneous
40+
41+
### CORS anywhere development proxy
42+
43+
The npm registry doesn't return any CORS headers nor can it be called with jsonp. So any in-browser XHR request will be blocked.
44+
45+
I made a little script that will proxy any request, adding those CORS headers to the response. It is automatically launched on `npm start`.
46+
47+
Note: `./bin/cors-anywhere.js` is for development purpose only, it may not be suited for production.

bin/cors-anywhere.js

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
#!/usr/bin/env node
2+
3+
/**
4+
* This file will open a server that will proxy ANY requests
5+
* and add CORS to the response headers.
6+
*
7+
* It is meant to be used for development purpose.
8+
*/
9+
10+
// Listen on a specific host via the HOST environment variable
11+
const host = process.env.HOST || "0.0.0.0";
12+
// Listen on a specific port via the PORT environment variable
13+
const port = process.env.PORT || 8000;
14+
15+
const corsProxy = require("cors-anywhere"); // eslint-disable-line
16+
17+
corsProxy
18+
.createServer({
19+
originWhitelist: [] // Allow all origins
20+
})
21+
.listen(port, host, () => {
22+
console.log(`Running CORS Anywhere on ${host}:${port}`);
23+
});

package-lock.json

+198
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+6-2
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,8 @@
1414
"warning": "^3.0.0"
1515
},
1616
"scripts": {
17-
"start": "react-scripts start",
17+
"start": "npm-run-all --parallel start:react proxy-npm-registry",
18+
"start:react": "react-scripts start",
1819
"build": "react-scripts build",
1920
"test": "npm run test:watch",
2021
"test:watch": "react-scripts test --env=jsdom",
@@ -23,17 +24,20 @@
2324
"eject": "react-scripts eject",
2425
"lint": "npx eslint .",
2526
"pretty": "npx prettier --write '**/*.{js,jsx,json,css,scss}'",
26-
"precommit": "lint-staged && npm run lint && npm run test:precommit"
27+
"precommit": "lint-staged && npm run lint && npm run test:precommit",
28+
"proxy-npm-registry": "node ./bin/cors-anywhere.js"
2729
},
2830
"lint-staged": {
2931
"**/*.{js,jsx,json,css,scss}": ["prettier --write", "git add"]
3032
},
3133
"devDependencies": {
34+
"cors-anywhere": "^0.4.1",
3235
"eslint-config-airbnb": "^15.1.0",
3336
"eslint-config-prettier": "^2.9.0",
3437
"eslint-plugin-prettier": "^2.6.0",
3538
"husky": "^0.14.3",
3639
"lint-staged": "^7.0.0",
40+
"npm-run-all": "^4.1.2",
3741
"prettier": "1.11.1"
3842
}
3943
}

0 commit comments

Comments
 (0)