Last active
December 29, 2015 01:09
-
-
Save ruzz311/7590970 to your computer and use it in GitHub Desktop.
simple proxy in node
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
{ | |
"name": "cor-request", | |
"version": "0.0.0", | |
"description": "", | |
"main": "server.js", | |
"scripts": { | |
"test": "echo \"Error: no test specified\" && exit 1" | |
}, | |
"author": "", | |
"license": "BSD", | |
"dependencies": { | |
"request": "~2.27.0", | |
"express": "~3.4.4" | |
} | |
} |
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
// npm init; npm install request express | |
var request = require('request'), | |
http = require('http'), | |
path = require('path'), | |
express = require('express'), | |
app = express(), | |
server = http.createServer(app), | |
serviceURL = 'http://your-url.com', | |
buildParams = function (obj) { | |
params = ''; | |
for (var key in obj) { params +='&'+key+'='+obj[key]; } | |
return params; | |
}; | |
app.set('port', 8000); | |
app.use(express.logger('dev')); | |
app.use(express.json()); | |
app.use(express.urlencoded()); | |
app.use(express.methodOverride()); | |
app.use(app.router); | |
app.use(express.directory(path.join(__dirname))); | |
app.use(express.static(path.join(__dirname))); | |
app.get('*', function (req, result) { | |
var params = buildParams(req.query), | |
url = serviceURL+req.route.params[0]+'?'+params; | |
request.get(url, function (err, data) { | |
result.send(JSON.parse(data.body)); | |
}); | |
}); | |
server.listen(app.get('port'), function () { | |
console.log('Express server listening on port ' + app.get('port')); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment