The Foundation
Welcome to the engine room. While frontend development is about what users see, backend development is about how the application actually works.
In this guide, we aren't just writing a script; we are building the foundation of a production-ready architecture. We will set up a Node.js server using Express, separate our configuration from our execution logic, and prepare our environment for professional development.
Why We Choose Express
You might wonder, "Why can't I just use raw Node.js?" You can, but it's like building a car from scratch every time you want to drive to the store.
- It's Minimalist: It gives you the basic tools (routing, middleware) without forcing a strict structure on you.
- It's the Standard: It is the most popular framework for Node.js, meaning you can find a library for almost anything you need.
- It's Fast: It adds very little overhead to your application.
Prerequisites:
To set up a basic Node.js server, you'll need to have Node.js installed on your machine. If you haven't installed it yet, you can download it from nodejs.org.
The Build: From Empty Folder to Running Server
Step 1. Create a New Directory:
Open your terminal and create a new directory for your project. Navigate into the directory.
mkdir my-node-server
cd my-node-serverStep 2. Initialize a New Node.js Project
Run the following command to create a package.json file:
npm init -yStep 3. Enable Modern JavaScript (ES Modules):
For this entire series, we will work with ES Modules (import/export) rather than the older CommonJS (require).
Open package.json and add "type": "module":
{
"name": "my-node-server",
"version": "1.0.0",
"type": "module",
"scripts": {},
"dependencies": {}
}Step 4. Install the Engine (Express & Nodemon):
Run the following command to install the Express framework and Nodemon.
npm install express
npm install -D nodemonNote: Nodemon is a development tool that automatically restarts your server whenever you save a file, saving you hours of manual restarting.
Step 4.5: The Golden Rule (.gitignore)
Before you commit anything, you must tell Git to ignore the node_modules folder. This folder can contain thousands of files and should never be shared.
Create a file named .gitignore in the root directory:
node_modules
.env
Step 5: Define the Application Logic (app.js)
We are going to separate our 'App Logic' from our 'Server Execution'.
- app.js: Defines what the app does (routes, middleware).
- server.js: Defines how the app runs (ports, database connections).
Create src/app.js:
import express from 'express';
const app = express();
app.get('/health', (req, res) => res.json({ status: 'ok' }));
export default app;Step 6: Ignite the Server (server.js)
Now we create the file that actually starts the machine. This file imports the app we just defined and tells it to listen on a specific port.
Create server.js in the root:
import app from './src/app.js';
const PORT = process.env.PORT || 3000;
app.listen(PORT, () => {
console.log(`Server is running on port ${PORT}`);
});⚠️Important: When using ES modules, file extensions (
.js) are required in imports.
Step 7: Automate the Startup
Open package.json and modify the scripts section to include a start script using Nodemon:
"scripts": {
"start": "nodemon server.js"
}Step 8: Liftoff
Run the following command to start your server:
npm startStep 9: Verification
Open your web browser or a tool like Postman and navigate to http://localhost:3000/health. You should see the following JSON response:
{
"status": "ok"
}What's Next?
Congratulations! You don't just have a script; you have a scalable server architecture. You've separated your concerns, set up your environment, and handled your first request.
But how does this server actually talk to the outside world? What exactly is an API?
In the next post, we’ll step back to understand the Request-Response Cycle, the role of the "Waiter," and the difference between architectures like REST and GraphQL.