Ejercicio 3. Mi primer módulo Node. Lectura de datos de la entrada estándar (STDIN). Familiarización con el objeto process

Mi primer módulo Node, el archivo surfaces.js:

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;
};

ej03.js:

function calcSurface(figure) {
    var surfaces = require ('./surfaces.js');

    process.stdin.resume();
    process.stdin.setEncoding('utf8');

    switch (figure) {
        case 'square':
            process.stdout.write('Please enter the side of a square: ');
            process.stdin.on('data', function (chunk) {
                var side = parseFloat(chunk);
                var surface = surfaces.square(side);
                process.stdout.write('The surface of the square is ' + surface + ' square meters\n');
                process.stdin.destroy();
            });
            break;

        case 'triangle':
            process.stdout.write('Please enter the base and the height of a triangle, separated by a space: ');
            process.stdin.on('data', function (chunk) {
                var baseAndHeight = chunk.split(' ');
                var base = parseFloat(baseAndHeight[0]);
                var height = parseFloat(baseAndHeight[1]);
                var surface = surfaces.triangle(base, height);
                process.stdout.write('The surface of the triangle is ' + surface + ' square meters\n');
                process.stdin.destroy();
            });
            break;

        case 'rectangle':
            process.stdout.write('Please enter the base and the height of a rectangle, separated by a space: ');
            process.stdin.on('data', function (chunk) {
                var baseAndHeight = chunk.split(' ');
                var base = parseFloat(baseAndHeight[0]);
                var height = parseFloat(baseAndHeight[1]);
                var surface = surfaces.rectangle(base, height);
                process.stdout.write('The surface of the rectangle is ' + surface + ' square meters\n');
                process.stdin.destroy();
            });
            break;

        case 'circle':
            process.stdout.write('Please enter the radius of a circle: ');
            process.stdin.on('data', function (chunk) {
                var surface = surfaces.circle(parseFloat(chunk));
                process.stdout.write('The surface of the circle is ' + surface + ' square meters\n');
                process.stdin.destroy();
            });
            break;

        default:
            process.stdout.write('You missed the option. Please try again.\n');
            process.exit();
            break;
    }
}

calcSurface(process.argv[2]);

process.on('exit', function() {
    process.stdout.write('Until next time my friend!\n');
});

Ejecución de ej03.js:

jordi@jordi-Aspire-5810T:~/nodes$ node ej03.js rectangle
Please enter the base and the height of a rectangle, separated by a space: 5.25 7.5
The surface of the rectangle is 39.375 square meters
Until next time my friend!
jordi@jordi-Aspire-5810T:~/nodes$ node ej03.js circle
Please enter the radius of a circle: 12
The surface of the circle is 452.3893421169302 square meters
Until next time my friend!
jordi@jordi-Aspire-5810T:~/nodes$ node ej03.js triangle
Please enter the base and the height of a triangle, separated by a space: 15 21
The surface of the triangle is 157.5 square meters
Until next time my friend!
jordi@jordi-Aspire-5810T:~/nodes$ node ej03.js square
Please enter the side of a square: 375.95
The surface of the square is 141338.4025 square meters
Until next time my friend!
jordi@jordi-Aspire-5810T:~/nodes$