scottfontenot
9/25/2017 - 9:37 PM

thinkful Unit 1: Server Side Fundamentals

thinkful Unit 1: Server Side Fundamentals

//Echo Endpoint

const express = require('express');
const app = express();

app.get('/echo/:what', function(req, res) {
  res.json({
    host: req.hostname,
    queryParams: req.query,
    params: req.params
  });
});

app.listen(process.env.PORT, () => {
  console.log(`Listening on port ${process.env.PORT}`);
});

///////////////////////////////////////////////
//Mad Libs Generator

'use strict';

//import express
const express = require('express');

//init a new express app
const app = express();

const doMadlib = (params) => {
  // we use destructuring to get the values for adjective1, adjective2, etc. from the request params
  const {adjective1, adjective2, adjective3, adverb, name, noun, place} = params;
  // then we return a string that substitutes in these values
  return (
  `There's a ${adjective1} new ${name} in ${place} and everyone's ` +
  `talking. Stunningly ${adjective2} and ${adverb} ${adjective3}, all the cool kids know it.` + 
  `However, ${name} has a secret - ${name}'s a vile vampire. \n` + 
  `Will it end with a bite, or with a stake through the ${noun}?`);
};

// GET requests to the root of the server, Pass the request query as params for doMadlib
app.get('/', (req, res) => res.send(doMadlib(req.query)));

// listen for requests :)
app.listen(process.env.PORT || 8080, () => console.log(
  `Your app is listening on port ${process.env.PORT || 8080}`));