Untitled

mail@pastecode.io avatar
unknown
plain_text
3 years ago
733 B
5
Indexable
Never
const express = require('express');
const app = express();
const PORT = 5000;
let alumnos = require('./alumnos.json').alumnos;
app.use(express.json());

const validatorMid = (req, res, next) => {
    const { id } = req.params;
    if(isNaN(id)){
       return res.status(400).json({msg: "Parametro incorrecto, debe ingresar un entero"}); 
    }
    next();
}

app.get('/alumnos/:id', validatorMid, function(req, res){
    const { id, nombre } = req.params;
    let alumno = alumnos.find( el => el.id == parseInt(id));
    if(!alumno) return res.status(404).json({msg: "usuario inexistente"})

    res.status(200).json({msg: "usuario", data:alumno})
});

app.listen(PORT, () => {
    console.log("running");
})