Last active
November 28, 2018 00:44
-
-
Save harrisonmalone/c69781edf1e9bfee0d232abd8b85b381 to your computer and use it in GitHub Desktop.
This file contains hidden or 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() | |
// allowed us to do post requests, middleware, will go into more tomorrow | |
app.use(express.json()) | |
const pokemon = [ | |
{ | |
id: 1, | |
name: 'pikachu', | |
}, | |
{ | |
id: 2, | |
name: 'raichu', | |
}, | |
{ | |
id: 52, | |
name: 'mewtwo' | |
} | |
] | |
app.get('/', (req, res) => { | |
return res.send('hello world') | |
}) | |
app.get('/pokemon', (req, res) => { | |
return res.send(pokemon) | |
}) | |
app.post('/pokemon', (req, res) => { | |
// res.send(req.body) | |
// get params from request body, used for strong params | |
const id = req.body.id | |
const name = req.body.name | |
// push into array | |
const poke = { | |
id: id, | |
name: name | |
} | |
pokemon.push(poke) | |
// send back to user new object created | |
return res.send(poke) | |
}) | |
app.get('/pokemon/:id', (req, res) => { | |
const id = parseInt(req.params.id) | |
const foundPoke = pokemon.find(poke => poke.id === id) | |
if (!foundPoke) { | |
return res.status(404).send('sorry pokemon not found') | |
} | |
return res.send(foundPoke) | |
}) | |
app.put('/pokemon/:id', (req, res) => { | |
// find the object in the json that we need to edit, we do this using params | |
const id = parseInt(req.params.id) | |
const foundPoke = pokemon.find(poke => poke.id === id) | |
// access the name that's sent up as a request, name is the key | |
// req.body gives you the body object of the request | |
const name = req.body.name | |
// assign the name that we've just stored to the foundPoke | |
foundPoke.name = name | |
if (!foundPoke) { | |
return res.status(404).send('sorry pokemon not found') | |
} | |
// send back the json that we've just updated | |
return res.send(foundPoke) | |
}) | |
app.delete('pokemon/:id', (req, res) => { | |
// find the object in the json that we need to delete, we do this using params | |
const id = parseInt(req.params.id) | |
const foundPoke = pokemon.find(poke => poke.id === id) | |
// send a 404 response if poke doesn't exist | |
if (!poke) return res.status(404).send('Pokemon not found!') | |
// get the actual index of poke in the array, it's not ordered in index so we can't use id | |
const index = pokemon.indexOf(poke) | |
// splice (remove) the poke from pokemon using the index we just found, it's like saying at index whatever take 1 | |
pokemon.splice(index, 1) | |
// send the poke as a response, which is kind of weird but works | |
return res.send(poke) | |
}) | |
app.listen(5000, () => console.log('listening on port 5000')) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment