Created
July 5, 2017 18:35
-
-
Save JoelCodes/67c5f62a4709cebcdec485e76343cd0c 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
| { | |
| "name": "w2d3", | |
| "version": "1.0.0", | |
| "description": "", | |
| "main": "index.js", | |
| "scripts": { | |
| "test": "echo \"Error: no test specified\" && exit 1" | |
| }, | |
| "keywords": [], | |
| "author": "", | |
| "license": "ISC", | |
| "dependencies": { | |
| "body-parser": "^1.17.2", | |
| "ejs": "^2.5.6", | |
| "express": "^4.15.3" | |
| }, | |
| "devDependencies": { | |
| "eslint": "^3.19.0", | |
| "eslint-config-airbnb": "^15.0.2", | |
| "eslint-plugin-import": "^2.6.1", | |
| "eslint-plugin-jsx-a11y": "^5.1.1", | |
| "eslint-plugin-react": "^7.1.0" | |
| } | |
| } |
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 bodyParser = require('body-parser'); | |
| const app = express(); | |
| app.set('view engine', 'ejs'); | |
| app.use(bodyParser.urlencoded({ | |
| extended: true | |
| })); | |
| const rando = () => { | |
| const letters = 'abcdefghijklmnopqrstuvwxyz' | |
| const alphabet = letters + letters.toUpperCase(); | |
| let output = ''; | |
| for(let i = 0; i < 6; i += 1 ){ | |
| output += alphabet[Math.floor(Math.random() * alphabet.length)]; | |
| } | |
| return output; | |
| } | |
| const hpDB = { | |
| a: { | |
| name: 'Cedric Diggory', | |
| year: 1995, | |
| position: 'Seeker' | |
| } | |
| } | |
| app.use('/hufflepuffs/:id', (req, res, next) => { | |
| req.hufflepuff = hpDB[req.params.id]; | |
| if(req.hufflepuff === undefined){ | |
| res.status(404).send('No hufflepuff here'); | |
| } else { | |
| next(); | |
| } | |
| }); | |
| app.get('/', (req, res) => { | |
| res.redirect('/hufflepuffs'); | |
| }); | |
| app.get('/hufflepuffs', (req, res) => { | |
| res.render('index', {hpDB}); | |
| }); | |
| app.get('/hufflepuffs/:id', (req, res) => { | |
| res.render('show', { | |
| hufflepuff: req.hufflepuff, | |
| hpID: req.params.id | |
| }); | |
| }); | |
| app.post('/hufflepuffs', (req, res) => { | |
| console.log(req.body); | |
| if(!req.body.name || !req.body.year || !req.body.position){ | |
| res.sendStatus(400); | |
| } else { | |
| const newId = rando(); | |
| hpDB[newId] = req.body; | |
| res.redirect('/hufflepuffs/' + newId); | |
| } | |
| }); | |
| app.post('/hufflepuffs/:id/delete', (req, res) => { | |
| delete hpDB[req.params.id]; | |
| res.redirect('/hufflepuffs'); | |
| }); | |
| app.listen(9001, () => { | |
| console.log('Listening on 9001'); | |
| }); |
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
| <!DOCTYPE html> | |
| <html lang="en"> | |
| <head> | |
| <meta charset="UTF-8"> | |
| <meta name="viewport" content="width=device-width, initial-scale=1.0"> | |
| <meta http-equiv="X-UA-Compatible" content="ie=edge"> | |
| <title>Document</title> | |
| </head> | |
| <body> | |
| <h1>Hufflepuffs!</h1> | |
| <ul> | |
| <% for(var hpID in hpDB){ %> | |
| <li><%= hpID %>: <a href='/hufflepuffs/<%= hpID %>'><%= hpDB[hpID].name %></a> (<%= hpDB[hpID].year %> <%= hpDB[hpID].position %>)</li> | |
| <% } %> | |
| </ul> | |
| <form method='POST' action='/hufflepuffs'> | |
| <p><label for="name">Name</label><input type="text" name='name'></p> | |
| <p><label for="year">Year</label><input type="number" name='year'></p> | |
| <p><label for="position">Position</label><input type="text" name='position'></p> | |
| <p><button type="submit">Submit</button></p> | |
| </form> | |
| </body> | |
| </html> |
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
| <!DOCTYPE html> | |
| <html lang="en"> | |
| <head> | |
| <meta charset="UTF-8"> | |
| <meta name="viewport" content="width=device-width, initial-scale=1.0"> | |
| <meta http-equiv="X-UA-Compatible" content="ie=edge"> | |
| <title>Document</title> | |
| </head> | |
| <body> | |
| <a href='/hufflepuffs/'>Back to Hufflepuffs</a> | |
| <h1><%= hufflepuff.name %></h1> | |
| <dl> | |
| <dd>Year</dd> | |
| <dt><%= hufflepuff.year %></dt> | |
| <dd>Position</dd> | |
| <dt><%= hufflepuff.position %></dt> | |
| </dl> | |
| <form method='POST' action='/hufflepuffs/<%= hpID %>/delete'> | |
| <button type='submit'>Delete <%= hufflepuff.name %></button> | |
| </form> | |
| </body> | |
| </html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment