Calculadora

package.json

{
    "name": "calculadora",
    "version": "1.0.0",
    "description": "",
    "main": "index.js",
    "scripts": {
        "test": "echo \"Error: no test specified\" && exit 1",
        "start": "node index.js",
        "dev": "nodemon"
    },
    "author": "",
    "license": "ISC",
    "devDependencies": {
        "nodemon": "^2.0.7"
    },
    "dependencies": {
        "express": "^4.17.1"
    }
}

index.js

const express = require("express");
const app = express();

var port = process.env.PORT || 8080;

// const c = require("./controller");
const { suma, resta, multiplicacion, division } = require("./controller");

//Middleware

// function logger(req, res, next) {
//     console.log(req.query.a);
//     console.log(req.query.b);
//     next();
// };

// function logger2(req, res, next) {
//     console.log(req._A);
//     console.log(req._B);
//     next();
// };

// Middlewares

function parseValues(req, res, next) {
    req._A = parseFloat(req.query.a.replace(",", "."))
    req._B = parseFloat(req.query.b.replace(",", "."))
    next();
};

function checkDivisor0(req, res, next){
    if (req._B === 0){
        next({ text: "imposible dividir por 0" , code: 3342})
    }
    next()
}

function errorHandler(error, req, res, next){
    res.status(500).json({code: error.code})
    console.log("error" , error)
    next(error)
}


// app.use(logger)
app.use(parseValues)
// app.use(logger2)

// app.get("/suma", myLogger, c.suma);
// app.get("/resta", myLogger,  c.resta);
// app.get("/multiplicacion",myLogger, c.multiplicacion);
// app.get("/division",myLogger, c.division);

app.get("/suma", suma);
app.get("/resta", resta);
app.get("/multiplicacion", multiplicacion);
app.get("/division", checkDivisor0, division);

app.use(errorHandler)


app.listen(port, () => {
    console.log(`http://localhost:${port}`);
});

controller

function suma(req, res){
    const result = req._A + req._B
    res.json({ result })
}

function resta(req, res){
    //const result = parseFloat(req.query.a.replace(",", ".")) - parseFloat(req.query.b.replace(",","."))
    const result = req._A - req._B
    res.json({ result })
}

function multiplicacion(req, res){
    // const result = parseFloat(req.query.a.replace(",", ".")) * parseFloat(req.query.b.replace(",","."))
    const result = req._A * req._B
    res.json({ result })
}

function division(req, res){
    //const result = parseFloat(req.query.a.replace(",", ".")) / parseFloat(req.query.b.replace(",","."))
    const result = req._A / req._B
    res.json({ result })
}

module.exports = { suma, resta, multiplicacion, division }