Two halves of every app
When you open a website, two programs cooperate.
- The frontend is the code running in your browser: the buttons, text, and colors you see.
- The backend is a program running on another computer, called a server, somewhere else in the world. It stores the data, checks passwords, and decides what each user is allowed to do.
The browser is called the client because it asks for things. The server answers. Every interaction follows the same cycle:
- The client sends a request: "give me the profile page for user 42".
- The server runs some code: look up user 42, check permissions.
- The server sends back a response: the data, plus a code saying how it went.
This course teaches you to write the program in the middle, using Node.js, a way to run JavaScript outside the browser.
Quiz
You type your password into a login form and press Submit. Which part of the app checks whether the password is correct?
A server is just a function
Strip away the network for a second. A backend is a function that takes a request and returns a response.
- A request has a method (like
GET, meaning "read something"), a path (like/hello), and sometimes data. - A response has a status code (a number:
200means OK,404means not found) and a body (the content).
You can write that function in plain JavaScript right now, no server needed. Run the example below. The handle function is the entire idea of a backend in ten lines.
Code exercise · javascript
Run this tiny pretend server. handle takes a request object and returns a response object. Press Run and check that both lines print.
Code exercise · javascript
Your turn. Add a second route: when the path is "/about", return status 200 with body "About us". Keep /hello and the 404 fallback working.