Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Updated to help when proxying locations that require credentials. #5

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,19 @@ http://localhost:8010/proxy/movies/list

End result will be a request to `https://www.yourdomain.ie/movies/list` without the CORS issues!

If you are proxying a location that requires credentials you'll find that you cannot allow origin * and send credentials.
To resolve this you can pass the specific origin you want to allow:

```
lcp --proxyUrl https://www.yourdomain.ie --allowedOrigin http://localhost:3000
```

Also when dealing with passing credentials, you may need to enable sending the include credentials header.
```
lcp --proxyUrl https://www.yourdomain.ie --allowedOrigin http://localhost:3000 --includeCredentials
```


Alternatively you can install the package locally and add a script to your project:

```json
Expand All @@ -51,3 +64,5 @@ Alternatively you can install the package locally and add a script to your proje
| --proxyUrl | https://www.google.ie | |
| --proxyPartial | foo | proxy |
| --port | 8010 | 8010 |
| --allowedOrigin | http://localhost:3000 | * |
| --includeCredentials | true | true |
14 changes: 12 additions & 2 deletions bin/lcp.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,25 @@ var optionDefinitions = [
type: String,
defaultValue: '/proxy'
},
{ name: 'proxyUrl', type: String }
{ name: 'proxyUrl', type: String },
{
name: 'allowedOrigin',
type: String,
defaultValue: '*'
},
{
name: 'includeCredentials',
type: Boolean,
defaultValue: false
}
];

try {
var options = commandLineArgs(optionDefinitions);
if (!options.proxyUrl) {
throw new Error('--proxyUrl is required');
}
lcp.startProxy(options.port, options.proxyUrl, options.proxyPartial);
lcp.startProxy(options.port, options.proxyUrl, options.proxyPartial, options.allowedOrigin, options.includeCredentials);
} catch (error) {
console.error(error);
}
15 changes: 12 additions & 3 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,15 @@ var cors = require('cors');
var chalk = require('chalk');
var proxy = express();

var startProxy = function(port, proxyUrl, proxyPartial) {
proxy.use(cors());

var startProxy = function(port, proxyUrl, proxyPartial, allowedOrigin, includeCredentials) {

var corsOptions = {
origin: allowedOrigin,
credentials: includeCredentials
}

proxy.use(cors(corsOptions));
proxy.options('*', cors());

// remove trailing slash
Expand All @@ -26,7 +33,9 @@ var startProxy = function(port, proxyUrl, proxyPartial) {
console.log(chalk.bgGreen.black.bold.underline('\n Proxy Active \n'));
console.log(chalk.blue('Proxy Url: ' + chalk.green(cleanProxyUrl)));
console.log(chalk.blue('Proxy Partial: ' + chalk.green(cleanProxyPartial)));
console.log(chalk.blue('PORT: ' + chalk.green(port) + '\n'));
console.log(chalk.blue('PORT: ' + chalk.green(port)));
console.log(chalk.blue('Allowed Origin: ' + chalk.green(allowedOrigin)));
console.log(chalk.blue('Include Credentials: ' + chalk.green(includeCredentials) + '\n'));
console.log(
chalk.cyan(
'To start using the proxy simply replace the proxied part of your url with: ' +
Expand Down
8 changes: 7 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,13 @@
"type": "git",
"url": "git+https://github.com/garmeeh/local-cors-proxy.git"
},
"keywords": ["cors", "proxy", "simple", "node", "express"],
"keywords": [
"cors",
"proxy",
"simple",
"node",
"express"
],
"bugs": {
"url": "https://github.com/garmeeh/local-cors-proxy/issues"
},
Expand Down