Created
April 25, 2017 23:08
-
-
Save bobbravo2/462c589235431eeedce414bdcb26b420 to your computer and use it in GitHub Desktop.
14.1.1 refactor
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
// Dependencies | |
var express = require("express"); | |
// Create express app instance. | |
var app = express(); | |
// Routes | |
app.get("/:operation/:firstNum/:secondNum", function(req, res) { | |
// Parameters are received from the URL | |
var operation = req.params.operation; | |
// Parameters are converted to integers | |
var firstNum = parseInt(req.params.firstNum); | |
var secondNum = parseInt(req.params.secondNum); | |
var result; | |
// Switch statement chooses operation based on the operation parameter. | |
switch (operation) { | |
case "add": | |
result = firstNum + secondNum; | |
break; | |
case "subtract": | |
result = firstNum - secondNum; | |
break; | |
case "multiply": | |
result = firstNum * secondNum; | |
break; | |
case "divide": | |
result = firstNum / secondNum; | |
break; | |
default: | |
result = "Sorry! The only valid operations are add, subtract, multiply, and divide."; | |
} | |
// We return the result back to the user in the form of a string | |
res.send(result.toString()); | |
}); | |
// Initiate the listener. | |
app.listen(3002); |
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": "expresscalculator", | |
"version": "1.0.0", | |
"description": "", | |
"main": "ExpressCalculator.js", | |
"scripts": { | |
"test": "echo \"Error: no test specified\" && exit 1" | |
}, | |
"author": "", | |
"license": "ISC", | |
"dependencies": { | |
"body-parser": "^1.15.2", | |
"express": "^4.14.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
// Dependencies | |
var express = require("express"); | |
// Create express app instance. | |
var app = express(); | |
// Routes | |
//What routes do you need to have? Which ones are optional? | |
//TODO Add your routes here | |
app.get("", function(req, res) { | |
//TODO parse out the variables from the request | |
// Parameters are received from the URL | |
//TODO make sure they're converted to integers (and not strings) | |
// Parameters are converted to integers | |
//Initialize the result variable to send later | |
var result; | |
// Switch statement chooses operation based on the operation parameter. | |
switch (operation) { | |
//BONUS - How could you use * + etc. inside the app.get()? | |
case "add": | |
//Add your logic here. Pun intended. | |
break; | |
case "subtract": | |
//Subtract logic | |
break; | |
case "multiply": | |
//Multiply | |
break; | |
case "divide": | |
//Divide | |
break; | |
default: | |
//Handle anything that isn't specified | |
result = | |
"Sorry! The only valid operations are add, subtract, multiply, and divide."; | |
} | |
// We return the result back to the user in the form of a string | |
res.send(result.toString()); | |
}); | |
// Initiate the listener. | |
app.listen(3002); |
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": "expresscalculator", | |
"version": "1.0.0", | |
"description": "", | |
"main": "ExpressCalculator.js", | |
"scripts": { | |
"test": "echo \"Error: no test specified\" && exit 1" | |
}, | |
"author": "", | |
"license": "ISC", | |
"dependencies": { | |
"body-parser": "^1.15.2", | |
"express": "^4.14.0" | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment