Skip to content

Instantly share code, notes, and snippets.

@gorkamu
Created May 14, 2017 18:31
Show Gist options
  • Save gorkamu/7058c04011c69a2d90b24901a651e629 to your computer and use it in GitHub Desktop.
Save gorkamu/7058c04011c69a2d90b24901a651e629 to your computer and use it in GitHub Desktop.
Ejemplo de métodos GET en una API REST con Node.js
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