A developer's Blog Log #5
...Jason Nutt 02/20/2021
Blog # 5, Creating a Route cont....
A re-re-iterated version of compiled notes & learning
From Treehouse, YouTube, DevEd, Node.js Docs and more...
Blog # 5, Creating a Route...cont...
Here is a link to learn how to do a cool typewriter effect with Ania Kubow
Inside app.js should look something like this
const express = require('express');
const app = express();
app.listen(3000);
Now let's get back to learning how to create routes in express
To create a route, I will use the get method on the app object
The get method is used to handle the get requests to a certain URL
For this example, we're going to create a route for the root route for our app
In other words, this is the route to follow if a visitor just visits our site without specifying a particular folder or file
For example, when you type in teamtreehouse.com you get treehouse's root route.
Then if I go to Stories, which is teamtreehouse.com/stories, you get the Success Stories for some of their students.
The root route is also one we visited when we got the 404
To indicate the site's root route we use the slash as the first parameter.
The first parameter is sometimes called the location parameter.
The second parameter of the get method is going to be an anonymous callback function.
This function is going to take two parameters, a request object And a response object.
This callback will run when the client requests this route.
We'll talk more about the request and response objects throughout this course.
For now, we're going to use the response's send method.
The send method sends a string to the client.
Now when we refresh the page, you notice we still get an error.
The reason is because even though we changed our code,
the express server is still serving up the old code.
The express server needs to be restarted to apply the new route, and the changes we've made
We can do this from the command line by hitting
CNTL + C
to stop the server
Since the server is stopped, we'll need to start it back up the same way we did the first time.
When we visit local host 3000 now, we see our application now works.
One more thing, if you don't have nodemon installed yet,
It is recommended that you do that for this course.
We'll be using it throughout this course to restart the express server.
It's also generally a good thing to have on hand for node development.
Without it, you'd be restarting the server every time you made a change to your code
which can get really old, really fast. To install it, just type npm
install -g nodemon
And hit Enter
Next