Ejercicio 4. Sencillo webservice calculador de áreas de figuras planas

El módulo Node surfaces.js (el mismo que el del ejercicio 3):

exports.square = function (s) {
    return s * s;
};

exports.rectangle = function (b, h) {
    return b * h;
};

exports.triangle = function (b, h) {
    return b * h / 2;
};

exports.circle = function (r) {
    return Math.PI * r * r;
};

exports.ellipse = function (a, b) {
    return Math.PI * a * b;
};

ej04.js:

var http = require('http');
var surfaces = require('./surfaces.js');

http.createServer(function (req, res) {

    var urlParams = req.url.split("/");
    var figure = urlParams[1];
    var op1 = parseFloat(urlParams[2]);
    var op2 = parseFloat(urlParams[3]);
    var surface;

    switch (figure) {
        case 'square':
            surface = surfaces.square(op1);
            break;

        case 'triangle':
            surface = surfaces.triangle(op1, op2);
            break;

        case 'rectangle':
            surface = surfaces.rectangle(op1, op2);
            break;

        case 'circle':
            surface = surfaces.circle(op1);
            break;

        default:
            surface = 'You missed the option. Please try again.\n';
            break;
    }

    res.writeHead(200, {'Content-Type': 'text/plain'});
    res.end("The surface of your " + figure + " is " + surface.toString() + " square meters\n");

}).listen(2012, "127.0.0.1");

console.log('Webservice running at http://127.0.0.1:2012/');

Ejecución de ej04.js:

jordi@jordi-Aspire-5810T:~/nodes$ node ej04.js &
[1] 3995
jordi@jordi-Aspire-5810T:~/nodes$ Webservice running at http://127.0.0.1:2012/

jordi@jordi-Aspire-5810T:~/nodes$ curl http://localhost:2012/square/8
The surface of your square is 64 square meters
jordi@jordi-Aspire-5810T:~/nodes$ curl http://localhost:2012/circle/8
The surface of your circle is 201.06192982974676 square meters
jordi@jordi-Aspire-5810T:~/nodes$ curl http://localhost:2012/triangle/8/2
The surface of your triangle is 8 square meters
jordi@jordi-Aspire-5810T:~/nodes$ curl http://localhost:2012/rectangle/8/2
The surface of your rectangle is 16 square meters
jordi@jordi-Aspire-5810T:~/nodes$