twhite96
8/12/2017 - 3:22 AM

Example Express app with route parameters

Example Express app with route parameters

var express = require("express");
var app = express();

app.get("/", function(req, res) {
  res.send("Hi, there, welcome to my assignment!");
});

app.get("/speak/:animal", function(req, res) {
  var animal = req.params.animal.toLowerCase();
  var sounds = {
    pig: "I make a great egg and toast companion!",
    cow: "Steaks are for losers. Try the chicken.",
    dog: "I am a stupid! I'll do anything you say! Sad!",
    cat: "I am the fire and the wind. Do not try to know me.",
    horse: "No, my name is not Ed."
  };

  var sound = sounds[animal];
  res.send("The " + animal + " says '" + sound + "'");
});

app.get("/repeat/:message/:times", function(req, res) {
  var message = req.params.message;
  var times = Number(req.params.times);
  var result = "";

  for (var i = 0; i < times; i++) {
    result += message + " ";
  }
  res.send(result);
});

app.get("*", function(req, res) {
  res.send("Sorry, page not found...What are you doing with your life?");
});

app.listen(process.env.PORT, process.env.IP, function() {
  console.log("App is listening!");
});