Skip to content

Instantly share code, notes, and snippets.

@davidpfahler
Created April 1, 2014 12:50
Show Gist options
  • Save davidpfahler/9913317 to your computer and use it in GitHub Desktop.
Save davidpfahler/9913317 to your computer and use it in GitHub Desktop.
Smartlaunch Proxy
var http = require('http'),
httpProxy = require('http-proxy');
process.on('uncaughtException', function(err) {
console.log(err)
})
// Create a proxy server with custom application logic
var proxy = httpProxy.createProxyServer({});
proxy.on('error', function (err, req, res) {
res.writeHead(500, {
'Content-Type': 'text/plain'
});
res.end('Something went wrong. And we are reporting a custom error message.');
});
// Create your custom server and just call `proxy.web()` to proxy
// a web request to the target passed in the options
// also you can use `proxy.ws()` to proxy a websockets request
var server = require('http').createServer(function(req, res) {
// You can define here your custom logic to handle the request
// and then proxy the request.
// Website you wish to allow to connect
res.setHeader('Access-Control-Allow-Origin', '*');
// Request methods you wish to allow
res.setHeader('Access-Control-Allow-Methods', 'GET, POST, OPTIONS, PUT, PATCH, DELETE');
// Request headers you wish to allow
res.setHeader('Access-Control-Allow-Headers', 'X-Requested-With,content-type');
// Set to true if you need the website to include cookies in the requests sent
// to the API (e.g. in case you use sessions)
res.setHeader('Access-Control-Allow-Credentials', true);
proxy.web(req, res, { target: 'http://localhost:7833' });
});
console.log("listening on port 8000")
server.listen(8000)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment