Skip to content

Commit

Permalink
changing CLI args + adding debug mode
Browse files Browse the repository at this point in the history
  • Loading branch information
ccoenraets committed Nov 4, 2014
1 parent 0a505ca commit 584fa05
Show file tree
Hide file tree
Showing 6 changed files with 116 additions and 141 deletions.
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
.idea
node_modules
.DS_Store
npm-debug.log
130 changes: 42 additions & 88 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,114 +1,68 @@
# force-server
# ForceServer

force-server is a simple development server for Force.com. It provides two main features:
ForceServer is a simple development server aimed at providing a simple and integrated developer experience when building applications that use Salesforce OAuth and REST services. ForceServer provides two main features:

- **A Proxy Server**: allows you to avoid cross domain policy issues when making REST API calls to Salesforce
- **A Local Web Server**: allows you to avoid cross domain policy issues when loading application resources using XMLHTTPRequest (templates, etc.)
- **A Proxy Server** to avoid cross-domain policy issues when invoking Salesforce REST services. (The Chatter API supports CORS, but other APIs don’t yet)
- **A Local Web Server** to (1) serve the OAuth callback URL defined in your Connected App, and (2) serve the whole app during development and avoid cross-domain policy issues when loading files (for example, templates) from the local file system.

There are different options to use force-server:
## Installing ForceServer

## Option 1: Install as a CLI

1. Install the force-server CLI
Open a command prompt and type:

```
npm install -g force-server
```

or (Unix-based systems)

```
sudo npm install -g force-server
```

1. Start the server

```
force-server [webRoot] [port]
```

- **webRoot**: path to the web root directory relative to the current directory. The default is the current directory.
- **port**: server port number. The default is 5000.

Examples:

To start force-server on port 5000 (default) and serve files in the current directory:
```
force-server
```

To start force-server on port 5000 (default) and serve files in the **www** directory (relative to the current directory):
```
force-server www
```

To start force-server on port 8000 and serve files in the **www** directory (relative to the current directory):
```
force-server www 8000
```

## Option 2: Install a local version

1. Clone this repository

```
git clone https://github.com/ccoenraets/force-server
```

1. Navigate to the force-server directory

```
cd force-server
```

1. Install the server dependencies

```
npm install
```

1. Start the server

```
node server [webRoot] [port]
sudo npm install -g force-server
```

## Sample App

## Option 3: Deploy to Heroku

[![Deploy](https://www.herokucdn.com/deploy/button.png)](https://heroku.com/deploy)
Create a file named index.html anywhere on you file system:

Because force-server is itself CORS-enabled, your application and the proxy don't have to be hosted on the same
domain.

## Proxy Server Usage

When making an API call using JavaScript (using XMLHTTPRequest, $.ajax, etc):

1. Substitute the actual service URL with the Proxy URL

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

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

1. Send the request as usual
```
[html]
<html>
<body>
<ul id="list"></ul>
<script src="http://ccoenraets.github.io/forcejs/force.js"></script>
<script>
force.login(function() {
force.query('select id, Name from contact LIMIT 50', function (response) {
var str = '';
for (var i = 0; i < response.records.length; i++) {
str += '<li>' + response.records[i].Name + '</li>';
}
document.getElementById('list').innerHTML = str;
});
});
</script>
</body>
</html>
[/html]
```

These steps are automated when using the [ForceJS](https://github.com/ccoenraets/forcejs) REST Library
## Run the Server

## Uninstalling the CLI
Navigate to the directory where you created index.html, and type:

To uninstall the CLI:

```
npm -g rm force-server
```
force-server
```

This command will start the server on port 8200, and automatically load your app (http://localhost:8200) in a browser window. You'll see the Salesforce login window, and the list of contacts will appear after you log in.

or
You can change the port number and the web root. Type the following command for more info:

```
sudo npm -g rm force-server
force-server --help
```

## Related Project
## Code Highlights

[ForceJS](https://github.com/ccoenraets/forcejs) is a REST Library for Force.com that works together with force-server to provide an integrated devlopment experience when building apps that connect to Salesforce using REST services.
1. The sample application above uses the <a href="">ForceJS</a> library. ForceJS and ForceServer are built to work closely together and provide an integrated developer experience.
1. ForceJS uses a default connected app: No need to create a connected app to start development. You should however create your own connected app for production use.
1. ForceServer automatically serves the OAuth callback URL: No need to create a callback HTML page during development.
2 changes: 1 addition & 1 deletion index.html
Original file line number Diff line number Diff line change
@@ -1 +1 @@
hello root
Hello Root!
12 changes: 12 additions & 0 deletions oauth/oauthcallback.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<html>
<body>
<script>
if (window.opener.force && window.opener.force.oauthCallback) {
window.opener.force.oauthCallback(window.location.href);
} else if (window.opener.oauthCallback) {
window.opener.oauthCallback(window.location.href);
}
window.close();
</script>
</body>
</html>
61 changes: 32 additions & 29 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,30 +1,33 @@
{
"name": "force-server",
"version": "0.0.5",
"description": "Development server for Force.com",
"bin": {
"force-server": "./bin/force-server.js"
},
"repository": {
"type": "git",
"url": "https://github.com/ccoenraets/force-server.git"
},
"keywords": [
"salesforce",
"force.com",
"proxy",
"cors",
"webserver"
],
"author": "Christophe Coenraets <ccoenraets@gmail.com> (http://coenraets.org/)",
"license": "MIT",
"bugs": {
"url": "https://github.com/ccoenraets/force-server/issues"
},
"homepage": "https://github.com/ccoenraets/force-server",
"dependencies": {
"express": "~4.9.8",
"body-parser": "~1.9.0",
"request": "~2.45.0"
}
}
"name": "force-server",
"version": "0.0.6",
"description": "Development server for Force.com",
"bin": {
"force-server": "./bin/force-server.js"
},
"repository": {
"type": "git",
"url": "https://github.com/ccoenraets/force-server.git"
},
"keywords": [
"salesforce",
"force.com",
"proxy",
"cors",
"webserver",
"forcejs"
],
"author": "Christophe Coenraets <ccoenraets@gmail.com> (http://coenraets.org/)",
"license": "MIT",
"bugs": {
"url": "https://github.com/ccoenraets/force-server/issues"
},
"homepage": "https://github.com/ccoenraets/force-server",
"dependencies": {
"body-parser": "~1.9.0",
"express": "~4.9.8",
"minimist": "^1.1.0",
"open": "0.0.5",
"request": "~2.45.0"
}
}
48 changes: 25 additions & 23 deletions server.js
Original file line number Diff line number Diff line change
@@ -1,30 +1,26 @@
var express = require('express'),
path = require('path'),
request = require('request'),
bodyParser = require('body-parser'),
open = require("open"),
argv = require('minimist')(process.argv.slice(2)),
app = express(),
webRoot = '',
port = 5000,
webRootFullPath;
root = argv.r || argv.root || '.',
port = argv.p || argv.port || '8200',
debug = argv.d || argv.debug || false;


if (process.argv[2]) {
webRoot = process.argv[2];
} else if (process.env.ROOT) {
webRoot = process.env.ROOT;
};

if (process.argv[3]) {
port = process.argv[3];
} else if (process.env.PORT) {
port = process.env.PORT;
};

webRootFullPath = path.join(process.cwd(), webRoot);
if (argv.h || argv.help) {
console.log('USAGE Example:');
console.log('force-server --port 8200 --root /users/chris/projects --debug');
return;
}

app.use(bodyParser.json());

app.use(express.static(webRootFullPath));
// Server application
app.use(express.static(root));

// Serve default oauthcallback.html during development if one is not available in root
app.use(express.static(__dirname + '/oauth'));

app.all('*', function (req, res, next) {

Expand All @@ -39,20 +35,26 @@ app.all('*', function (req, res, next) {
} else {
var targetURL = req.header('Target-URL');
if (!targetURL) {
res.status(500).send({ error: 'There is no Target-Endpoint header in the request' });
res.status(500).send({ error: 'Resource Not Found (Web Server) or no Target-Endpoint header in the request (Proxy Server)' });
return;
}
request({ url: targetURL + req.url, method: req.method, json: req.body, headers: {'Authorization': req.header('Authorization')} },
var url = targetURL + req.url;
if (debug) console.log(req.method + ' ' + url);
if (debug) console.log('Request body:');
if (debug) console.log(req.body);
request({ url: 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);
if (debug) console.log('Response body:');
if (debug) console.log(body);
}).pipe(res);
}
});

app.listen(port, function () {
console.log('force-server listening on port ' + port);
console.log('web root: ' + webRootFullPath);
console.log('Root: ' + root);
open("http://localhost:" + port);
});

0 comments on commit 584fa05

Please sign in to comment.