Skip to content

Instantly share code, notes, and snippets.

@exwar
Forked from mistakster/api.js
Last active August 29, 2015 14:24
Show Gist options
  • Select an option

  • Save exwar/945ece22c677b9393afe to your computer and use it in GitHub Desktop.

Select an option

Save exwar/945ece22c677b9393afe to your computer and use it in GitHub Desktop.

Revisions

  1. @mistakster mistakster created this gist Jul 12, 2015.
    7 changes: 7 additions & 0 deletions api.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,7 @@
    var express = require('express');
    var router = express.Router();

    router.use('/playlists', require('./playlists'));
    router.use('/songs', require('./songs'));

    module.exports = router;
    8 changes: 8 additions & 0 deletions index.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,8 @@
    var express = require('express');
    var app = express();

    app.set('json spaces', 2);

    app.use('/api', require('./api'));

    app.listen(3000);
    41 changes: 41 additions & 0 deletions playlists.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,41 @@
    var express = require('express');
    var router = express.Router();

    var playlists = [
    { id: 1, title: 'Rock', songs: [4,5,6,7] },
    { id: 2, title: 'Pop', songs: [1,2,3] },
    { id: 3, title: 'Jazz', songs: [8,9,10,11,12] }
    ];

    router.get('/', function (req, res) {
    res.json(playlists);
    });

    router.param('id', function (req, res, next, id) {
    var item = playlists.filter(function (item) {
    return item.id == id;
    });
    req.playlistItem = item.length ? item[0] : null;
    next();
    });

    router.route('/:id')
    .get(function (req, res, next) {
    return req.playlistItem ? res.json(req.playlistItem) : next();
    })
    .post(function (req, res) {
    // create item
    })
    .put(function (req, res) {
    // update item
    })
    .delete(function (req, res) {
    // delete item
    });


    // handle "/api/playlists/1/songs/" as well
    router.use('/:id/songs', require('./songs'));


    module.exports = router;
    58 changes: 58 additions & 0 deletions songs.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,58 @@
    var express = require('express');
    var router = express.Router();

    var songs = [
    { id: 1, artist: 'Madonna', title: 'Living for Love' },
    { id: 2, artist: 'Madonna', title: 'Ghosttown' },
    { id: 3, artist: 'Madonna', title: 'Like a Prayer' },
    { id: 4, artist: 'Red Hot Chili Peppers', title: 'Californication' },
    { id: 5, artist: 'Red Hot Chili Peppers', title: 'Under the Bridge' },
    { id: 6, artist: 'Bruce Springsteen', title: 'Born in the U.S.A.' },
    { id: 7, artist: 'Bruce Springsteen', title: 'Dancing in the Dark' },
    { id: 8, artist: 'Miles Davis', title: 'Blue in Green' },
    { id: 9, artist: 'Miles Davis', title: 'So What' },
    { id: 10, artist: 'Nina Simone', title: 'Feeling Good' },
    { id: 11, artist: 'Nina Simone', title: 'My Baby Just Cares for Me' },
    { id: 12, artist: 'Nina Simone', title: 'I Put a Spell on You' }
    ];

    function filterByPlaylist(playlist, songs) {
    if (playlist) {
    songs = songs.filter(function (song) {
    return playlist.songs.some(function (songId) {
    return songId == song.id;
    });
    });
    }
    return songs;
    }

    router.get('/', function (req, res) {
    res.json(filterByPlaylist(req.playlistItem, songs));
    });

    router.param('id', function (req, res, next, id) {
    var item = filterByPlaylist(req.playlistItem, songs)
    .filter(function (item) {
    return item.id == id;
    });
    req.songItem = item.length ? item[0] : null;
    next();
    });

    router.route('/:id')
    .get(function (req, res, next) {
    return req.songItem ? res.json(req.songItem) : next();
    })
    .post(function (req, res) {
    // create item
    })
    .put(function (req, res) {
    // update item
    })
    .delete(function (req, res) {
    // delete item
    });


    module.exports = router;