Routes Lab
Frasier
Make sure you shut down your previous app
be sure to have
control cyour previous app if you were running one before starting this new activity
Basic Express App
Create a new directory called "frasier"
cdintofrasierperform an npm init, specify
server.jsas your entry point (HINT: you'll either have to do this in the initialnpm initprocess, or make a change to the default settings in thepackage.json.install
expresscreate a
server.jsfileinside
server.js, require express and save it to a variable namedexpresscreate a variable named app, and set it equal to
express()have app listen on port 3000
once it's listening, log "Hello, Seattle. I'm listening..."
test it by going to http://localhost:3000
Routes
create a GET route for
/frasierit should send a response of "Once in prep school, the Existentialist Club once named me 'Most Likely to Be'"
test the
/frasierroute by going to http://localhost:3000/frasiercreate a GET route for
/nilesit should send a response of "You know, sometimes I wonder if I'm not just in psychiatry for the money"
test the /niles route by going to http://localhost:3000/niles
create a GET route for
/daphneit should send a response of "Well, have fun. I'm off to stick my head in the oven."
test the
/daphneroute by going to http://localhost:3000/daphnecreate a GET route for
/rozit should send a response of "Let me make it easy for you. Freaks! Freaks on line 1. Freaks on line 2! Freaks! Everywhere!"
test the
/rozroute by going to http://localhost:3000/rozcreate a GET route for
/martinit should send a response of "You may think it's tough being middle-aged, but think about me. I got a son who's middle-aged."
test the
/martinroute by going to http://localhost:3000/martin
Are URLs case sensitive? Let's test: 1. create a GET route for /Scrappy
it should send a response of "woof"
test the
/Scrappyroute by going to...
http://localhost:3000/scrappy - what happened?
http://localhost:3000/Scrappy - what happened?
Simpsons
Refer back to previous section and notes/demos for steps, if necessary
cdback to your student labs dirCreate a directory called "simpsons"
Get a basic express server running
Install express
No routes
Just have the app listen
Test to make sure it works in the browser
Create a route for Homer
Test the Homer route
Create a route for Marge
Test the Marge route
Create a route for Bart
Test the Bart route
Create a route for Lisa
Test the Lisa route
Create a route for Maggie
Test the Maggie route
Create a route for Snowball II (spaces won't work, so just make it one word or add dashes in between each word)
Test the Snowball II route
Create a route for Santa's Little Helper (apostrophes won't work, so just remove it in the route)
Test the Santa's Little Helper route
HTML site
The topic is yours. Try to do it all from memory
Create a basic app with multiple routes
When it comes time to do res.send, instead of just text, send html
To get res.send to work with line breaks in the html, change single/double quotes to backticks
res.send(` <html> <body> <h1>Oh ha!</h1> </body> </html> `);
Hungry For More
Some of you will zip through this lab
Work on practicing Bootstrap (see morning exercise for an activity that you'll work on after lunch as well
Learn about JavaScript's Event Loop this is great stuff but you'll likely need to watch it a few times to get it all down
Try the super bonus!
This lab was adapted from SEI-MAE
Last updated
Was this helpful?