Created
May 14, 2017 18:31
-
-
Save gorkamu/7058c04011c69a2d90b24901a651e629 to your computer and use it in GitHub Desktop.
Ejemplo de métodos GET en una API REST con Node.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var Album = require('../models/album'); | |
function getAlbum(req, res){ | |
var albumId = req.params.id; | |
Album.findById(albumId, function(err, album) { | |
if(err) { | |
res.status(500).send({message: 'Error 500 al devolver el album'}); | |
}else{ | |
if(!album){ | |
res.status(404).send({message: 'No hay album'}); | |
}else{ | |
res.status(200).send(album); | |
} | |
} | |
}); | |
} | |
function getAlbumes(req, res){ | |
Album.find({}).sort('-_id').exec((err, albumes) => { | |
if(err){ | |
res.status(500).send({message: 'Error 500 al devolver los albumes'}); | |
}else{ | |
if(!albumes){ | |
res.status(404).send({message: 'No hay albumes'}); | |
}else{ | |
res.status(200).send({albumes: albumes}) | |
} | |
} | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment