Let's build our first dynamic web application! We'll be using the Express framework, a lightweight way to write web application
Originally, the web was invented to share hypertext documents, mostly among academics. People would write HTML files โ HTML stands for Hypertext Markup Language โ and serve them up using a simple document server (called a web server). Users would navigate between web servers using a browser, a special piece of software that could turn HTML into something graphical and clickable.
Web browsers and web servers communicate using HTTP, the Hypertext Transfer Protocol.
A web application is a program that listens for incoming HTTP requests and responds with a programmatically-generated HTML document.
- Fork this repository
- Use
git clone
to get a local copy of your fork - Run
npm install
inside the project directory - Run
npm start
to start the web application - Navigate to http://localhost:3000
- Start on the iterations
Remember, you have to run npm install
before any of this will work.
-
You can restart the web server by pressing
Ctrl+C
in the terminal where the web server is running. -
If you see an error involving
EADDRINUSE
that means you're already running the web server somewhere. You have to kill that copy of the server.To do that, run the following:
ps aux | grep app.js
You should see a line that contains
node app.js
, like this:jesse 40313 0.0 0.1 4571396 24564 s000 S+ 8:26AM 0:00.15 node app.js
The second column is the process ID. Run the following to kill that process:
kill 40313
Before you write any code, get the web server running and visit http://localhost:3000. Click around and explore. When you visit a page, look at app.js
and see what the web application is doing.
Pay attention to how the web application...
- Knows which block of code to run
- Creates the HTML document
- Sends the HTML document back to the browser
Make sure you understand how the web application handles /bake
before attempting this.
- As a user, when I visit
/greet?name=WXYZ
, I see a message greetingWXYZ
.WXYZ
could be any string, e.g.,/greet?name=Jesse
,/greet?name=Wilfred
. - As a user, when I visit
/greet
, I see a message telling me there's no greeting to give.
(Again, look at the form on /bake
) to see how it works.
- As a user, when I visit
/greet
(with or without parameters), I see a form that allows me to enter a name and submit to see a new greeting.
Add your own page that has dynamic behavior. Servers can read/write to files, so maybe think of information you want to remember between page loads.
For example, you could read the contents of a todos.txt
and display a list on the page.
Or, you could display the current time.
Or, you could keep track of how many people visited the page.
Or, you could use the data in the URL to dynamically set the styles on the page. Maybe change the background color or the typeface?