Skip to content

Instantly share code, notes, and snippets.

@amscotti
Last active December 15, 2015 09:09
Show Gist options
  • Save amscotti/5236433 to your computer and use it in GitHub Desktop.
Save amscotti/5236433 to your computer and use it in GitHub Desktop.
Proxy server for working with Yeoman and a REST API. Install http-proxy by running 'npm install http-proxy' or add to your package.json and run 'npm install'
var httpProxy = require('http-proxy'),
staticDir = 'app',
apiHost = '<Your API Host>',
apiPort = 80,
apiPath = '/api';
var proxy = new httpProxy.RoutingProxy();
connect()
.use(connect.logger("dev"))
.use(function (req, res, next) {
if (req.url.indexOf(apiPath) === 0) {
proxy.proxyRequest(req, res, {
host: apiHost,
port: apiPort
});
} else {
next();
}
})
.use(connect.static(staticDir))
.listen(process.env.PORT || 8000);
console.log("Loading Server at http://localhost:" + (process.env.PORT || 8000));
@unludo
Copy link

unludo commented Jul 16, 2013

I am wondering how to use your gist. The else case is for providing the yeoman static content? So the request is proxied to another server?
Also, I don't see staticDir being used.

Thank you

@amscotti
Copy link
Author

amscotti commented Oct 1, 2013

I've updated the code, I was doing things backwards before. Should be clear and straightforward.

If the URL matches what you set for the endpoint in this case /api then it will be proxy to the API server else it will be read from the static server.

I found with the setup I so need to run grunt server for all the files will still get built and updated as I work on the project.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment