Skip to content

Instantly share code, notes, and snippets.

@bullgare bullgare/server.js Secret

Created Oct 2, 2013
Embed
What would you like to do?
touch node.js proxy
var apiHost = 'm.bolgov.dev.lan',
touchPath = '/Users/bolgov/projects/whole_mamba/touch/app/',
port = 3500,
vars = {
paths: {static: '/', templates: '/views/'}, // http://images.wambacdn.net/images/touch/
tplSuffix: '',
cacheVersion: 1,
PAGE_images_server: 'http://images.wambacdn.net/images/',
language: {sname: 'ru'},
domains: {default: 'mamba.ru'},
total: {anketa_online: 100},
os: {ios: '', android: 1, wp7: ''},
env: {devel: true}
},
express = require('express'),
http = require('http'),
path = require('path'),
fs = require('fs'),
app = express();
function renewCacheVersion()
{
"use strict";
vars.cacheVersion = Math.round(Math.random() * 100000000);
}
// app.use(express.logger());
app.get('/', function(req, res){
renewCacheVersion();
proxyFile(req, res, 'index.html', 'text/html', parseIndex);
});
//var contentTypes = {
// js: 'text/javascript',
// css: 'text/css'
//};
app.get(/.+\.html(\?v=\d+)?$/, function (req, res) {
proxyFile(req, res, req.url, 'text/html', function (content, callback) {
callback(replaceBlitzTranslations(content));
});
});
app.get(/.+\.js(\?v=\d+)?$/, function (req, res) {
proxyFile(req, res, req.url, 'text/javascript');
});
app.get(/.+\.css(\?v=\d+)?$/, function (req, res) {
proxyFile(req, res, req.url, 'text/css');
});
app.get(/.+\.(png|jpg|gif)(\?v=\d+)?$/, function (req, res) {
proxyFile(req, res, req.url, 'image/' + req.params[0]);
});
app.all('*', function (req, res) {
// var proxy = http.createClient(80, req.headers['host']);
// var proxy = http.request(80, apiHost);
// var proxy_request = proxy.request(req.method, req.url, req.headers);
delete req.headers.host;
var options = {
hostname: apiHost,
port: 80,
path: req.url,
method: req.method,
headers: req.headers
},
proxy = http.request(options);
// @debug
console.log(options);
proxy.on('response', function (proxyResponse) {
proxyResponse.pipe(res);
res.writeHead(proxyResponse.statusCode, proxyResponse.headers);
});
req.pipe(proxy);
});
function proxyFile(req, res, url, ctype, parser)
{
"use strict";
var options = {};
if (parser) {
options.encoding = 'utf-8'
}
fs.readFile(path.join(touchPath, url.replace(/\?v=.*$/, '').replace('..', '')), options, function (err, data) {
if (err)
{
res.writeHead(404);
res.end();
}
else
{
res.writeHead(200, {'Content-Type': ctype});
if (parser !== undefined)
{
parser(data, function (data) {
res.end(data);
})
}
else {
res.end(data);
}
}
});
}
function parseIndex(content, callback)
{
"use strict";
content = replaceBlitzTranslations(content);
var replacers = [];
recursiveObjToArray(vars, replacers, '');
for (var i = 0, replacer; i < replacers.length; i ++)
{
replacer = replacers[i];
// replace all server vars
content = content.replace(new RegExp('\\{\\{\\s*' + replacer.k + '\\s*\\}\\}', 'g'), replacer.v);
// replace all server if statements (if, if-else, unless)
content = content.replace(new RegExp('\\{\\{\\s*IF\\s*' + replacer.k + '\\s*\\}\\}([\\s\\S]+?)((\\{\\{\\s*ELSE\\s*\\}\\})([\\s\\S]+?))?\\{\\{\\s*END\\s*\\}\\}', 'mg'), function (s, r1, r2, r3, r4) {
return replacer.v ? r1 : (r4 ? r4 : '');
});
// content = content.replace(new RegExp('\\{\\{\\s*IF\\s*' + replacer.k + '\\s*\\}\\}([\\s\\S]+?)\\{\\{\\s*END\\s*\\}\\}', 'mg'), function (s, p1) {
// return replacer.v ? p1 : '';
// });
content = content.replace(new RegExp('\\{\\{\\s*UNLESS\\s*' + replacer.k + '\\s*\\}\\}([\\s\\S]+?)\\{\\{\\s*END\\s*\\}\\}', 'mg'), function (s, p1) {
return replacer.v ? '' : p1;
});
}
// all unknown vars are empty
content = content.replace(new RegExp('\\{\\{.*}\\}', 'g'), '');
callback(content);
}
function replaceBlitzTranslations(content)
{
return content.replace(/\{\{\s*_\(\s*'/g, '').replace(/\{\{\s*asis\(\s*'/g, '').replace(/'\s*\)\s*\}\}/g, '');
}
function recursiveObjToArray(obj, arr, prefix)
{
"use strict";
if (prefix === undefined) {
prefix = '';
}
if (! isObject(obj)) {
return obj;
}
for (var i = 0, keys = Object.keys(obj), l = keys.length, k, v; i < l; i ++)
{
k = keys[i];
v = obj[k];
if (isObject(v)) {
recursiveObjToArray(v, arr, combine(prefix, k));
}
else {
arr.push({k: combine(prefix, k), v: v});
}
}
return arr;
}
function isObject(v)
{
"use strict";
return v != null && typeof v == 'object';
}
/**
* combines prefixes to new one
* @param {String} prefix
* @param {String} key
* @return {String}
*/
function combine(prefix, key)
{
"use strict";
if (prefix.length) {
return prefix + '.' + key;
}
return key;
}
app.listen(port);
console.log('Express server started on port %s', port);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.