Last active
December 15, 2015 09:09
-
-
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'
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); |
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
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