├── .gitignore ├── app.js ├── models └── tvshow.js ├── package.json └── routes └── tvshows.js /.gitignore: -------------------------------------------------------------------------------- 1 | node_modules -------------------------------------------------------------------------------- /app.js: -------------------------------------------------------------------------------- 1 | var express = require("express"), 2 | app = express(), 3 | http = require("http"), 4 | server = http.createServer(app), 5 | mongoose = require('mongoose'); 6 | 7 | app.configure(function () { 8 | app.use(express.bodyParser()); 9 | app.use(express.methodOverride()); 10 | app.use(app.router); 11 | }); 12 | 13 | app.get('/', function(req, res) { 14 | res.send("Hello world!"); 15 | }); 16 | 17 | routes = require('./routes/tvshows')(app); 18 | 19 | mongoose.connect('mongodb://localhost/tvshows', function(err, res) { 20 | if(err) { 21 | console.log('ERROR: connecting to Database. ' + err); 22 | } else { 23 | console.log('Connected to Database'); 24 | } 25 | }); 26 | 27 | server.listen(3000, function() { 28 | console.log("Node server running on http://localhost:3000"); 29 | }); -------------------------------------------------------------------------------- /models/tvshow.js: -------------------------------------------------------------------------------- 1 | var mongoose = require('mongoose'), 2 | Schema = mongoose.Schema; 3 | 4 | var tvshowSchema = new Schema({ 5 | title: { type: String }, 6 | year: { type: Number }, 7 | country: { type: String }, 8 | poster: { type: String }, 9 | seasons: { type: Number }, 10 | genre: { type: String, enum : 11 | ['Drama', 'Fantasy', 'Sci-Fi', 'Thriller', 'Comedy'] 12 | }, 13 | summary: { type: String } 14 | }); 15 | 16 | 17 | module.exports = mongoose.model('TVShow', tvshowSchema); -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name" : "node-api-rest-example", 3 | "version" : "0.0.1", 4 | "dependencies" : { 5 | "express" : "3.2.6", 6 | "mongoose" : "3.6.11" 7 | } 8 | } -------------------------------------------------------------------------------- /routes/tvshows.js: -------------------------------------------------------------------------------- 1 | //File: routes/tvshows.js 2 | module.exports = function(app) { 3 | 4 | var TVShow = require('../models/tvshow.js'); 5 | 6 | //GET - Return all tvshows in the DB 7 | findAllTVShows = function(req, res) { 8 | TVShow.find(function(err, tvshows) { 9 | if(!err) { 10 | console.log('GET /tvshows') 11 | res.send(tvshows); 12 | } else { 13 | console.log('ERROR: ' + err); 14 | } 15 | }); 16 | }; 17 | 18 | //GET - Return a TVShow with specified ID 19 | findById = function(req, res) { 20 | TVShow.findById(req.params.id, function(err, tvshow) { 21 | if(!err) { 22 | console.log('GET /tvshow/' + req.params.id); 23 | res.send(tvshow); 24 | } else { 25 | console.log('ERROR: ' + err); 26 | } 27 | }); 28 | }; 29 | 30 | //POST - Insert a new TVShow in the DB 31 | addTVShow = function(req, res) { 32 | console.log('POST'); 33 | console.log(req.body); 34 | 35 | var tvshow = new TVShow({ 36 | title: req.body.title, 37 | year: req.body.year, 38 | country: req.body.country, 39 | poster: req.body.poster, 40 | seasons: req.body.seasons, 41 | genre: req.body.genre, 42 | summary: req.body.summary 43 | }); 44 | 45 | tvshow.save(function(err) { 46 | if(!err) { 47 | console.log('Created'); 48 | } else { 49 | console.log('ERROR: ' + err); 50 | } 51 | }); 52 | 53 | res.send(tvshow); 54 | }; 55 | 56 | //PUT - Update a register already exists 57 | updateTVShow = function(req, res) { 58 | TVShow.findById(req.params.id, function(err, tvshow) { 59 | tvshow.title = req.body.petId; 60 | tvshow.year = req.body.year; 61 | tvshow.country = req.body.country; 62 | tvshow.poster = req.body.poster; 63 | tvshow.seasons = req.body.seasons; 64 | tvshow.genre = req.body.genre; 65 | tvshow.summary = req.body.summary; 66 | 67 | tvshow.save(function(err) { 68 | if(!err) { 69 | console.log('Updated'); 70 | } else { 71 | console.log('ERROR: ' + err); 72 | } 73 | res.send(tvshow); 74 | }); 75 | }); 76 | } 77 | 78 | //DELETE - Delete a TVShow with specified ID 79 | deleteTVShow = function(req, res) { 80 | TVShow.findById(req.params.id, function(err, tvshow) { 81 | tvshow.remove(function(err) { 82 | if(!err) { 83 | console.log('Removed'); 84 | } else { 85 | console.log('ERROR: ' + err); 86 | } 87 | }) 88 | }); 89 | } 90 | 91 | //Link routes and functions 92 | app.get('/tvshows', findAllTVShows); 93 | app.get('/tvshow/:id', findById); 94 | app.post('/tvshow', addTVShow); 95 | app.put('/tvshow/:id', updateTVShow); 96 | app.delete('/tvshow/:id', deleteTVShow); 97 | 98 | } --------------------------------------------------------------------------------