-
-
Save Marak/618971 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
var sys = require('sys'), | |
eyes = require('eyes'), | |
url = require('url'), | |
querystring = require('querystring'), | |
httpProxy = require('http-proxy'); | |
exports.start = function() { | |
var static = require('node-static'); | |
// Create a node-static server to serve the current directory | |
var file = new(static.Server)('./public', { cache: 7200, headers: {'X-Hello':'World!'} }); | |
require('http').createServer(function (request, response) { | |
request.body = ''; | |
// proxy api requests to listening master | |
if(request.url.search('/api') > -1) { | |
sys.puts('trying pipe message to api'); | |
request.url = request.url.replace('/api', ''); | |
var proxy = new httpProxy.HttpProxy(request, response); | |
proxy.proxyRequest(9001, 'localhost', request, response); | |
return; | |
} | |
request.addListener('data',function(chunk){ | |
request.body += chunk | |
}); | |
request.addListener('end', function () { | |
var params = querystring.parse(url.parse(request.url).query); | |
var posted = querystring.parse(request.body); | |
sys.puts(request.body); | |
if(request.url.search('/signups') > -1){ | |
/* | |
Perform signup | |
*/ | |
response.writeHead(200, {"Content-Type":"text/html"}); | |
response.write('foo'); | |
response.end(); | |
}); | |
} | |
else if(request.url.search('/login') > -1) { | |
/* | |
Attempt login | |
*/ | |
sys.puts('trying to login'); | |
if(posted.password == "foo") { | |
response.writeHead(200, {"Content-Type":"application/json"}); | |
response.write('{"result":true}'); | |
response.end(); | |
} | |
else { | |
response.writeHead(200, {"Content-Type":"application/json"}); | |
response.write('{"result":false}'); | |
response.end(); | |
} | |
} | |
else{ | |
if(request.url == '/'){ | |
request.url = "index.html"; | |
} | |
file.serve(request, response, function (err, res) { | |
if (err) { // An error as occured | |
sys.error("> Error serving " + request.url + " - " + err.message); | |
response.writeHead(err.status, err.headers); | |
response.end(); | |
} else { // The file was served successfully | |
sys.puts("> " + request.url + " - " + res.message); | |
} | |
}); | |
} | |
}); | |
}).listen(8080); | |
sys.puts("> client is listening on http://127.0.0.1:8080"); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment