Skip to content
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
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# Ignore node_modules folder
node_modules
26 changes: 20 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,26 @@ There are different options to get your own instance of the CORS proxy up and ru
- Start the server

```
node server
npm start
```

2. To run with Typescript
- Intall typescript
```
npm install -g typescript
```
- Install the server dependencies
```
npm install
```
- Compile typescipt
```
npm run compile
```
- Start the server
```
npm run start-ts
```

## Usage

When making an API call using JavaScript (using XMLHTTPRequest, $.ajax, etc):
Expand All @@ -37,7 +54,7 @@ When making an API call using JavaScript (using XMLHTTPRequest, $.ajax, etc):

1. Set the request method, query parameters, and body as usual

1. Set the actual service URL in a header named 'Target-Endpoint'
1. Set the actual service URL in a header named 'Target-URL'

1. Send the request as usual

Expand All @@ -54,6 +71,3 @@ The proxy currently passes the "Authorization" header to the target endpoint. Yo
additional headers (or all of them).


## Other Implementations

Check out James Ward's [Saleforce CORS Proxy](https://github.com/jamesward/sf-cors-proxy) written in Scala.
13 changes: 13 additions & 0 deletions cors-proxy-ts/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { Server } from './server-ts';

const bodyLimit: string = typeof (process.argv[2]) != 'undefined' ? process.argv[2] : '100kb';
console.log('Using limit: ',bodyLimit);
export const app = Server.bootstrap(bodyLimit).app;
app.set('port', process.env.PORT || 3000);
app.listen(app.get('port'), function (err) {
if (!err)
console.log('Proxy server listening on port ' + app.get('port'));
else {
console.error(err)
}
})
61 changes: 61 additions & 0 deletions cors-proxy-ts/server-ts.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@

import * as express from 'express';
import * as request from 'request';
import * as bodyParser from 'body-parser';

export class Server {
public static bootstrap(bodyLimit: string): Server {
return new Server(bodyLimit);
}

public app: express.Application;

private constructor(bodyLimit: string) {
// create expressjs application
this.app = express();

// configure application
this.config(bodyLimit);
this.enableCors();
}
private config(bodyLimit: string) {
// add body parser
this.app.use(bodyParser.json({ limit: bodyLimit }));

// mount query string parser
this.app.use(
bodyParser.urlencoded({
extended: true,
}),
);
}
private enableCors() {
//console.log("inside cors")
this.app.use(function (req, res, next) {
// Set CORS headers: allow all origins, methods, and headers: you may want to lock this down in a production environment
res.header("Access-Control-Allow-Origin", "*");
res.header("Access-Control-Allow-Methods", "GET, PUT, PATCH, POST, DELETE");
res.header("Access-Control-Allow-Headers", req.header('access-control-request-headers'));

if (req.method === 'OPTIONS') {
// CORS Preflight
res.send();
} else {
const targetURL = req.header('Target-URL');
if (!targetURL) {
res.status(500).send({ error: 'There is no Target-Endpoint header in the request' });
return;
}
request({ url: targetURL + req.url, method: req.method, json: req.body, headers: { 'Authorization': req.header('Authorization') } },
function (error, response, body) {
if (error) {
console.error('error: ' + response.statusCode)
}
// console.log(body);
}).pipe(res);

}
});
}
}

Loading