What Really Happens Behind a Website? My First Look at the Backend
I can build a website that looks good and feels alive. A form takes my input, I press submit, and the data vanishes. But where does it go? "The backend," you might say. Fair, but how does it get there? A week ago, I co
I can build a website that looks good and feels alive. A form takes my input, I press submit, and the data vanishes.
But where does it go?
"The backend," you might say. Fair, but how does it get there? A week ago, I couldn't have told you. So I ran my first Node.js server to find out. This post is what I learned, including the mistakes.
Frontend vs. Backend
The frontend is everything you can see: the sign-in form, the buttons, the layout. The backend is everything else, working out of sight: the server, the API, the database, and the rules that connect them.
Whenever you open a website, your browser (the client) sends a request to a server. The server does its work and sends a response back. That's the client-server model, and nearly everything on the web follows it.
Take logging in. You type your details and press the button. The backend checks them against records in the database and replies with either "Welcome!" or "Wrong password."
Do you always need a database? No. Only when the backend has something to save.
Why Node.js and Not Just JavaScript?
JavaScript was originally built to run inside the browser. Node.js is a runtime built on Google's V8 engine that lets JavaScript run outside the browser. It's not a new language, just another place JavaScript can run.
I chose it because I can use one language for both frontend and backend. Node.js also comes with npm, a huge library of free code shared by developers, so you install what you need instead of writing everything yourself.
Building My First Node.js Server
Step 1: Install Node.js
Download the LTS version from nodejs.org, then check it worked:
node -v
npm -v
If both show version numbers, you're ready.
Step 2: Create a project
mkdir my-first-backend
cd my-first-backend
npm init -y
This makes a folder and a package.json file, which stores your project's details and dependencies.
Step 3: Write the server
Create a file named server.js:
const http = require('http');
const server = http.createServer((req, res) => {
res.writeHead(200, { 'Content-Type': 'text/plain' });
res.end('Hello from my first Node.js server!');
});
server.listen(3000, () => {
console.log('Server running at http://localhost:3000');
});
-
httpis a built-in Node.js module. -
createServer()makes a server that runs your function for every request. -
reqis the request, andresis what you send back. -
res.end()sends the message and finishes the response. -
listen(3000)waits for visitors on port 3000. A port is like a numbered door on your computer.
Step 4: Run it
node server.js
The terminal keeps running. That's normal, because the server is waiting for requests. Press Ctrl + C to stop it.
Step 5: Open your browser
Visit http://localhost:3000. You should see:
Hello from my first Node.js server!
Seeing my own server answer my browser made everything click.
What actually happened?
- The browser sent an HTTP request to my server.
- Node.js ran my function.
- The server sent back a response, and the browser showed it.
localhost means my own computer, so only I can open it. A deployed server runs on a remote machine so anyone can reach it.
5 Common Problems I Faced (and How I Fixed Them)
1. EADDRINUSE: port already in use
I ran node server.js and got an error saying port 3000 was already in use. I had forgotten that my old server was still running in another terminal. I stopped it with Ctrl + C and ran the command again.
If you can't find the old terminal, kill the process:
# Windows
netstat -ano | findstr :3000
taskkill /PID <number> /F
You can also just use a different port, like `3001`.
### 2. `Cannot find module`
Node told me it couldn't find my file. The cause was a small typo in the file name, and I was also in the wrong folder.
bash
node sever.js # wrong
node server.js # right
npm install express # install packages before using them
Now I check the spelling and run `ls` (or `dir` on Windows) to confirm I'm in the right folder.
### 3. Connection refused
My browser showed "This site can't be reached." The server wasn't running, so nothing was listening. I started it again and the page loaded.
bash
node server.js
Also make sure the port in the browser (`localhost:3000`) matches the port in your code.
### 4. No error handling
I didn't think about this at first, because my server only returned one message. But one bad request can crash the whole server, so I now wrap risky code in `try/catch`.
js
const server = http.createServer((req, res) => {
try {
const data = JSON.parse('not valid json');
res.end('ok');
} catch (err) {
res.writeHead(500);
res.end('Something went wrong.');
}
});
Now the user gets a clear message and the server keeps running.
### 5. Hardcoding the port and secrets
I wrote `3000` directly in my code. That works on my laptop, but hosting services choose the port for you, and secrets like passwords should never sit in your code.
js
const PORT = process.env.PORT || 3000;
server.listen(PORT);
Secrets go in a `.env` file, and I add that file to `.gitignore` so it never reaches GitHub.
## Where Does Express Come In?
As apps grow, handling every request by hand gets repetitive. **Express** is a framework that makes building servers and APIs easier. It isn't built in, so install it first:
bash
npm install express
js
const express = require('express');
const app = express();
app.get('/', (req, res) => {
res.send('Welcome to the homepage!');
});
app.listen(3000);
`app.get('/')` means "when someone visits the homepage, run this function."
## What I Learned
I started this journey with one question: where does my data go?
Now I know. It goes to a server, a program that waits for requests and answers them. My first one only said βHello,β but it was mine, and it was running on my own laptop.
Next stop: REST APIs and MongoDB. Iβm not afraid of the backend anymore. Iβm curious about it.
Originally published by Dev.to WebDev. Aggregated on AIWithGhost for educational purposes β full credit and traffic to the original publisher.
