Skip to content

Instantly share code, notes, and snippets.

@michal
Last active August 29, 2015 14:25
Show Gist options
  • Save michal/0837052dbb06dfbf48a3 to your computer and use it in GitHub Desktop.
Save michal/0837052dbb06dfbf48a3 to your computer and use it in GitHub Desktop.
{
"httpRoot": "/",
"httpPort": 8080,
"serviceRoot": "/api/",
"serviceHost": "localhost",
"useLocal": true,
"staticPath": ["apps", "library"]
}
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