Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
This is another version of a bad implementation
// blockerbal.js
/*
This code is a bad example! It will
block the server. It is for demonstration
only.
Arguments: port number
*/
var restify = require('restify');
var fs = require('fs');
var params = process.argv.splice(2);
var server = restify.createServer();
var fibonacci = function (n) {
if (n < 2)
return 1;
else
return fibonacci(n-2) + fibonacci(n-1);
};
// Home page:
server.get('/',function(req, res) {
var bodyHtml = '<!DOCTYPE html><html><head><title>'
+ 'Teste Node.js - O Bom Programador</title></head>'
+ '<body>'
+ '<br/>Porta: ' + params[0]
+ '<br/>Ok, agora, <a href="/blocker">o blocker</a>';
bodyHtml += '</code></pre></body></html>';
res.writeHead(200, {
'Content-Length': Buffer.byteLength(bodyHtml),
'Content-Type': 'text/html'
});
res.write(bodyHtml);
res.end();
});
// Blocker page:
server.get('/blocker',function(req, res) {
var inicio = new Date();
var bodyHtml = '<!DOCTYPE html><html><head><title>'
+ 'Teste Node.js - O Bom Programador</title></head>'
+ '<body>'
+ '<br/>Porta: ' + params[0];
var resultado = fibonacci(45);
var fim = new Date();
bodyHtml += '<br/>' + resultado;
bodyHtml += '<br/>inicio: ' + inicio + ' fim: ' + fim;
bodyHtml += '</code></pre></body></html>';
res.writeHead(200, {
'Content-Length': Buffer.byteLength(bodyHtml),
'Content-Type': 'text/html'
});
res.write(bodyHtml);
res.end();
});
// Start server
server.listen(params[0], function() {
console.log('Online: ' + params[0]);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment