CS212-Projects-express/server.js
cdez 0f2b3d0ef8 Homework 8 Completion
++ Homework 8 Files
+- Modified server.js
2023-04-10 21:37:40 -07:00

44 lines
1.6 KiB
JavaScript

const express = require('express')
const logger = require('morgan')
const path = require('path')
const server = express()
server.use(express.urlencoded({'extended': true}))
server.use(logger('dev'))
// Routes
server.get('/do_a_random', (req, res) => {
res.send(`Your number is: ${Math.floor(Math.random() * 100) + 1}`)
})
server.post("/cs212/homework/8/index.html", (req, res) => {
// Retrieve form data from request body
ingredientOneInput = req.body.ingredientOne;
ingredientTwoInput = req.body.ingredientTwo;
ingredientThreeInput = req.body.ingredientThree;
ingredientFourInput = req.body.ingredientFour;
ingredientFiveInput = req.body.ingredientFive;
ingredientSixInput = req.body.ingredientSix;
ingredientSevenInput = req.body.ingredientSeven;
// Create mad lib string
madLib = `A throw together soup consisting of: One cup of ${ingredientOneInput}, Five cups of ${ingredientTwoInput}, Two ${ingredientThreeInput}, 1/2 cup of ${ingredientFourInput}, One large ${ingredientFiveInput}, 1/4 pound of ${ingredientSixInput}, and ${ingredientSevenInput} to taste`;
// Send mad lib as response
res.send(madLib);
});
// Setup static page serving for all the pages in "public"
const publicServedFilesPath = path.join(__dirname, 'public')
server.use(express.static(publicServedFilesPath))
// The server uses port 80 by default unless you start it with the extra
// command line argument 'local' like this:
// node server.js local
let port = 8191
if (process.argv[2] === 'local') {
port = 80
}
server.listen(port, () => console.log('Ready on localhost!'))