Skip to content

Instantly share code, notes, and snippets.

@hideki
Created April 26, 2011 08:34
Show Gist options
  • Save hideki/941985 to your computer and use it in GitHub Desktop.
Save hideki/941985 to your computer and use it in GitHub Desktop.
simple power function demo using Node on cloudfoundry.com
var http = require('http');
var url = require('url');
var host = process.env.VCAP_APP_HOST || 'localhost';
var port = process.env.VCAP_APP_PORT || 3000;
var pow = function(x, n) {
if(n < 0){
return 0;
}
if(n === 0){
return 1;
}
var temp = pow(x, Math.floor(n/2));
if(n % 2 === 0){
return temp * temp;
}
else{
return temp * temp * x;
}
};
http.createServer(function (req, res) {
res.writeHead(200, {'Content-Type': 'text/html'});
res.write('<html><body>');
res.write('<h1>Power function with Node.js</h1>');
var urlObj = url.parse(req.url, true);
var uri = urlObj.pathname;
if(uri === "/pow") {
var query = urlObj.query;
var base = parseInt(query.base);
var exponent = parseInt(query.exponent);
if(base && exponent){
res.write('pow('+base+', '+exponent+') = ' + pow(base, exponent));
res.write('<hr/>');
}
}
res.write('<form method="get" action="/pow">');
res.write('<table>');
res.write('<tr><td>base</td><td><input type="text" name="base"/></td></tr>');
res.write('<tr><td>exponent</td><td><input type="text" name="exponent"/></td></tr>');
res.write('</table>');
res.write('<input type="submit" value="pow"/>');
res.write('</form>');
res.end('</body></html>');
}).listen(port, null);
console.log('Server running at http://' + host + ':' + port + '/');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment