-
-
Save exwar/945ece22c677b9393afe to your computer and use it in GitHub Desktop.
This file contains 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 express = require('express'); | |
var router = express.Router(); | |
router.use('/playlists', require('./playlists')); | |
router.use('/songs', require('./songs')); | |
module.exports = router; |
This file contains 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 express = require('express'); | |
var app = express(); | |
app.set('json spaces', 2); | |
app.use('/api', require('./api')); | |
app.listen(3000); |
This file contains 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 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; |
This file contains 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 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; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment