-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathproxy.js
66 lines (51 loc) · 2.08 KB
/
proxy.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
/////////////////////////////////////////////////////////////////////////
// __ ___ _______ ___
// / |/ /__ ___ ____ _ /_ __(_)__ __ __ / _ \_______ __ ____ __
// / /|_/ / -_) _ `/ _ `/ / / / / _ \/ // / / ___/ __/ _ \\ \ / // /
// /_/ /_/\__/\_, /\_,_/ /_/ /_/_//_/\_, / /_/ /_/ \___/_\_\\_, /
// /___/ /___/ /___/
//
// A fun tiny proxy for when you barely care.
// v0.0.1
////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////
// Setup
//////////////////////////////////////////
var express = require('express'),
app = express(),
httpProxy = require('http-proxy'),
request = require('request'),
colors = require('colors');
var allowCrossDomain = function(req, res, next) {
res.header('Access-Control-Allow-Origin', '*');
res.header('Access-Control-Allow-Methods', 'GET,PUT,POST,DELETE,OPTIONS');
res.header('Access-Control-Allow-Headers', 'Content-Type, Authorization, Content-Length, X-Requested-With');
// intercept OPTIONS method
if ('OPTIONS' == req.method) {
res.send(200);
}
else {
next();
}
};
function reportProxy(req, url){
console.log('proxying', req.originalUrl.blue ,'to', url.magenta);
}
//////////////////////////////////////////
// Config
/////////////////////////////////////////
app.use(allowCrossDomain);
//////////////////////////////////////////
// Forwarding Rules
//////////////////////////////////////////
app.use('/api/collections/:collId', function(req, res) {
var url = "http://tiny-pizza-server.herokuapp.com/collections/" + req.params.collId;
reportProxy(req, url);
req.pipe(request(url)).pipe(res);
});
//////////////////////////////////////////
// Go for the Gold!
//////////////////////////////////////////
app.listen(3000);
console.log("Hey!".red + " Welcome to ".blue + "Mega Tiny Proxy".yellow + "!".blue + "\
\nWaiting to make your dreams come true...".green + "♡".magenta);