Created
August 20, 2012 08:32
-
-
Save Anks/3402241 to your computer and use it in GitHub Desktop.
Easy local development with a node.js proxy
This file contains 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
{ | |
"name": "your-app-name", | |
"version": "0.0.1", | |
"private": true, | |
"dependencies": { | |
"http-proxy": "0.8.x", | |
"connect": "2.3.x" | |
} | |
} |
This file contains 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'), | |
connect = require('connect'), | |
endpoint = { | |
host: 'your-app-domain.com', // or IP address | |
port: 80, | |
prefix: '/api' | |
}, | |
staticDir = 'public'; | |
var proxy = new httpProxy.RoutingProxy(); | |
var app = connect() | |
.use(connect.logger('dev')) | |
.use(function(req, res) { | |
if (req.url.indexOf(endpoint.prefix) === 0) { | |
proxy.proxyRequest(req, res, endpoint); | |
} | |
}) | |
.use(connect.static(staticDir)) | |
.listen(4242); | |
// http://localhost:4242/api/test will give response | |
// from http://your-app-domain.com/api/test |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment