Last active
August 29, 2015 14:25
-
-
Save michal/0837052dbb06dfbf48a3 to your computer and use it in GitHub Desktop.
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
{ | |
"httpRoot": "/", | |
"httpPort": 8080, | |
"serviceRoot": "/api/", | |
"serviceHost": "localhost", | |
"useLocal": true, | |
"staticPath": ["apps", "library"] | |
} |
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
var express = require('express'); | |
var path = require('path'); | |
var fs = require('fs'); | |
var bodyParser = require('body-parser'); | |
var request = require('request'); | |
var app = express(); | |
var dataPath = __dirname + '/data/'; | |
var rootPath = __dirname + '/../'; | |
var configFile = require(rootPath + 'apps/config/app.json'); | |
app.use(bodyParser.json()); | |
var run = function(address, file) { | |
app.all(configFile.serviceRoot + address, function(req, res) { | |
res.json(JSON.parse(fs.readFileSync(dataPath + file + '.json', 'utf8'))); | |
}); | |
}; | |
if (configFile.useLocal) { | |
run('', 'test'); | |
} else { | |
app.use(configFile.serviceRoot, function(req, res) { | |
var url = configFile.serviceHost + req.url; | |
var query = false; | |
if (req.method === 'POST') { | |
query = request.post({ | |
uri: url, | |
json: req.body | |
}); | |
} else { | |
query = request(url); | |
} | |
req.pipe(query).pipe(res); | |
}); | |
} | |
configFile.staticPath.forEach(function(value) { | |
app.use('/' + value, express.static(rootPath + value)); | |
}); | |
app.get('/', function(req, res) { | |
res.sendFile(path.join(rootPath + 'index.html')); | |
}); | |
app.listen(configFile.httpPort); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment