-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
47 lines (39 loc) · 1.31 KB
/
server.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
/**
* Module dependencies.
*/
function startServer() {
var express = require('express'),
http = require('http'),
path = require('path'),
app = express(),
request = require('request'),
_ = require('lodash');
function querify(queryParamsObject){
return '?'+_.map(queryParamsObject || {}, function(val, key){
return key+'='+val
}).join('&');
}
// adds a new rule to proxy a localUrl -> webUrl
// i.e. proxify ('/my/server/google', 'http://google.com/')
function proxify(localUrl, webUrl){
app.get(localUrl, function(req, res) {
var url = [
webUrl,
querify(req.query)
].join("");
req.pipe(request(url)).pipe(res);
});
}
// add your proxies here.
//
// examples:
// proxify('/yummly/recipes', 'http://api.yummly.com/v1/api/recipes');
// proxify('/brewery/styles', 'https://api.brewerydb.com/v2/styles');
// all environments
app.set('port', process.argv[3] || process.env.PORT || 3000);
app.use(express.static(path.join(__dirname, '')));
http.createServer(app).listen(app.get('port'), function() {
console.log('Express server listening on port ' + app.get('port'));
});
}
module.exports.startServer = startServer;