Skip to content

Instantly share code, notes, and snippets.

View gorkamu's full-sized avatar
🏠
Working from home

0xGorkamu gorkamu

🏠
Working from home
  • Zaragoza
View GitHub Profile
@gorkamu
gorkamu / album.js
Created May 14, 2017 19:48
Modelo de datos para el objeto Álbum de nuestra API REST con Node.js
'use strict'
var mongoose = require('mongoose');
var AlbumSchema = new mongoose.Schema({
title: String,
band: String,
genre: String,
year: String,
});
@gorkamu
gorkamu / album.js
Last active March 8, 2021 14:57
Ejemplo de método DELETE en una API REST con Node.js
function deleteAlbum(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){
album.remove(err => {
if(err){
@gorkamu
gorkamu / album.js
Last active May 14, 2017 19:00
Ejemplo de método PUT en una API REST con Node.js
function updateAlbum(req, res){
var params = req.body;
var albumId = req.params.id;
Album.findByIdAndUpdate(albumId, params, function(err, album){
if(err){
res.status(500).send({message: 'Error 500 al actualizar el album'});
}else{
res.status(200).send(album);
}
@gorkamu
gorkamu / album.js
Last active May 14, 2017 18:51
Ejemplo de método POST en una API REST con Node.js
function saveAlbum(req, res){
var album = new Album();
var params = req.body;
album.title = params.title;
album.band = params.band;
album.genre = params.genre;
album.year = params.year;
album.save(function(err, albumStored){
@gorkamu
gorkamu / album.js
Created May 14, 2017 18:31
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){
@gorkamu
gorkamu / index.js
Last active May 2, 2017 09:19
Example of node js entrypoint with mongoose configuration
'use strict'
var mongoose = require('mongoose');
var app = require('./app');
var port = process.env.PORT || 3000;
// Puerto por defecto de mongodb
mongoose.connect('mongodb://localhost:27017/base-de-datos-de-prueba', function(err, res){
if(err){
throw err;
@gorkamu
gorkamu / album.js
Created May 1, 2017 10:38
Example of routes file in Node.js
'use strict'
var express = require('express');
var AlbumController = require('../controllers/album');
var api = express.Router();
api.get('/album/:id', AlbumController.getAlbum);
api.get('/album', AlbumController.getAlbums);
api.post('/album', AlbumController.saveAlbum);
api.put('/album', AlbumController.updateAlbum);
@gorkamu
gorkamu / album.js
Created May 1, 2017 10:21
Example of controller in Node.js
'use strict'
function getAlbum(req, res){
var id = req.params.id;
res.status(200).send({
data: id,
});
}
function getAlbums(req, res){
@gorkamu
gorkamu / app.js
Created May 1, 2017 10:09
Core fle of out node.js app
'use strict'
var express = require('express');
var bodyParser = require('body-parser');
var app = express();
var api = require('./routes/favorito');
// Convierte una petición recibida (POST-GET...) a objeto JSON
app.use(bodyParser.urlencoded({extended:false}));
@gorkamu
gorkamu / index.js
Created May 1, 2017 10:03
Entry point of a node.js application
'use strict'
var app = require('./app');
var port = process.env.PORT || 3000;
app.listen(port, () => {
console.log(`Server running in http://localhost:${port}`);
console.log('Defined routes:');
console.log(' [GET] http://localhost:3000/');
});