Created
December 17, 2018 17:51
-
-
Save johnayoung/c4d50cfd64e0dda30abd49ed0eb7dacb 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
| 'use strict'; | |
| // Request and response object drills | |
| // ================================== | |
| const express = require('express'); | |
| const app = express(); | |
| // use express middleware to parse the request body and add it to the request object | |
| // don't worry, you'll learn all about middleware in the next assignment! | |
| app.use(express.json()); | |
| const logObj = function (req, res) { | |
| return { | |
| body: req.body | |
| }; | |
| }; | |
| const madLib = function(body) { | |
| return `There's a ${body.adjective1} new ${body.name} | |
| in ${body.place} and everyone's talking. | |
| Stunningly ${body.adjective2} and ${body.adverb} ${body.adjective3}, | |
| all the cool kids know it. | |
| However, ${body.name} has a secret - ${body.name}'s a vile vampire. | |
| Will it end with a bite, or with a stake through the ${body.noun}?`; | |
| }; | |
| const obj = { | |
| 'adjective1': 'red', | |
| 'adjective2': 'spikey', | |
| 'adjective3': 'effective', | |
| 'adverb': 'rapidly', | |
| 'name': 'Joe', | |
| 'place': 'Kansas', | |
| 'noun': 'bungee cord' | |
| }; | |
| console.log(madLib(obj)); | |
| // your code here. | |
| app.post('/', (req, res) => { | |
| const body = req.body; | |
| const madLib2 = madLib(body); | |
| console.log(req.body); | |
| // console.log(req.body); | |
| res.json(madLib(req.body)); | |
| console.log(madLib2); | |
| }); | |
| // hint: in Postman under Body remember to select JSON instead of Text, then try doing | |
| // console.log(req.body) here to show your key-value pairs from Postman in the Logs | |
| // listen for requests :) | |
| app.listen(process.env.PORT, () => | |
| console.log(`Your app is listening on port ${process.env.PORT}`) | |
| ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment