-
-
Save codecademydev/bb6abf042f5405d8f7c5b377986c1ada to your computer and use it in GitHub Desktop.
Codecademy export
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
const express = require('express'); | |
const app = express(); | |
const morgan = require('morgan'); | |
const bodyParser = require('body-parser'); | |
app.use(express.static('public')); | |
const PORT = process.env.PORT || 4001; | |
const cards = [ | |
{ | |
id: 1, | |
suit: 'Clubs', | |
rank: '2' | |
}, | |
{ | |
id: 2, | |
suit: 'Diamonds', | |
rank: 'Jack' | |
}, | |
{ | |
id: 3, | |
suit: 'Hearts', | |
rank: '10' | |
} | |
]; | |
let nextId = 4; | |
// Logging | |
if (!process.env.IS_TEST_ENV) { | |
app.use(morgan('short')); | |
} | |
// Parsing | |
app.use(bodyParser.json()); | |
// Find card | |
app.use('/cards/:cardId', (req, res, next) => { | |
const cardId = Number(req.params.cardId); | |
const cardIndex = cards.findIndex(card => card.id === cardId); | |
if (cardIndex === -1) { | |
return res.status(404).send('Card not found'); | |
} | |
req.cardIndex = cardIndex; | |
next(); | |
}); | |
const validateCard = (req, res, next) => { | |
const newCard = req.body; | |
const validSuits = ['Clubs', 'Diamonds', 'Hearts', 'Spades']; | |
const validRanks = ['2', '3', '4', '5', '6', '7', '8', '9', '10', 'Jack', 'Queen', 'King', 'Ace']; | |
if (validSuits.indexOf(newCard.suit) === -1 || validRanks.indexOf(newCard.rank) === -1) { | |
return res.status(400).send('Invalid card!'); | |
} | |
next(); | |
}; | |
// Get all Cards | |
app.get('/cards/', (req, res, next) => { | |
res.send(cards); | |
}); | |
// Create a new Card | |
app.post('/cards/', validateCard, (req, res, next) => { | |
const newCard = req.body; | |
newCard.id = nextId++; | |
cards.push(newCard); | |
res.status(201).send(newCard); | |
}); | |
// Get a single Card | |
app.get('/cards/:cardId', (req, res, next) => { | |
res.send(cards[req.cardIndex]); | |
}); | |
// Update a Card | |
app.put('/cards/:cardId', validateCard, (req, res, next) => { | |
const newCard = req.body; | |
const cardId = Number(req.params.cardId); | |
if (!newCard.id || newCard.id !== cardId) { | |
newCard.id = cardId; | |
} | |
cards[req.cardIndex] = newCard; | |
res.send(newCard); | |
}); | |
// Delete a Card | |
app.delete('/cards/:cardId', (req, res, next) => { | |
cards.splice(req.cardIndex, 1); | |
res.status(204).send(); | |
}); | |
// Start the server | |
app.listen(PORT, () => { | |
console.log(`Server is listening on port ${PORT}`); | |
}); |
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
$(document).ready(function() { | |
$('#send-button').on('click', function(e) { | |
path = $('#path').val(); | |
if (path.charAt(0) === '/') { | |
path = path.substring(1); | |
} | |
const method = $('#verb').val(); | |
if (method === 'POST' || method === 'PUT') { | |
const body = { | |
rank: $('#rank').val(), | |
suit: $('#suit').val() | |
}; | |
$.ajax({ | |
method: method, | |
url: path, | |
data: JSON.stringify(body), | |
contentType: 'application/json', | |
success: handleSuccess, | |
error: function(jqxhr) { | |
$('#status-code').text(jqxhr.status); | |
$('#response-body').text(''); | |
} | |
}); | |
} else { | |
$.ajax({ | |
method: method, | |
url: path, | |
success: handleSuccess, | |
error: function(jqxhr) { | |
$('#status-code').text(jqxhr.status); | |
$('#response-body').text(''); | |
} | |
}); | |
} | |
e.preventDefault(); | |
}); | |
}); | |
function handleSuccess(response, status, jqxhr) { | |
$('#status-code').text(jqxhr.status); | |
$('#response-body').text(JSON.stringify(response, null, 4)); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment