-
Notifications
You must be signed in to change notification settings - Fork 0
/
proxy-factory.js
45 lines (36 loc) · 1.05 KB
/
proxy-factory.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
var request = require( "request" );
var extend = require( "extend" );
var queryString = require( "query-string" );
var def = {
protocol: "http",
host: "localhost",
port: 8000
};
module.exports = function( app, defaults ) {
var config = extend( def, defaults );
var proxy = {
get: mapping( "GET" ),
post: mapping( "POST" ),
put: mapping( "PUT" ),
delete: mapping( "DELETE" ),
del: mapping( "DELETE" ),
patch: mapping( "PATCH" ),
option: mapping( "OPTION" ),
all: mapping( "ALL" )
};
function mapping( method ) {
return function( path ) {
app[ method.toLowerCase() ]( path, function( req, res ) {
var destiny = config.protocol + "://" + config.host + ":" + config.port + req.path;
if ( req.query ) {
destiny += "?" + queryString.stringify( req.query );
}
console.log( req.method + "\t" + destiny );
req.url = destiny;
req.body = req.body ? JSON.stringify( req.body ) : null;
request( req ).pipe( res );
});
};
}
return proxy;
};