How to Create an Express Server From Scratch
Creating an Express server is the first step of a Node.js backend. Here is how to do it from scratch.
How to Create an Express Server From Scratch
Creating an Express server is the first real step of a Node.js backend. Here is the step-by-step guide.
Step 1: Initialize the Project
Run npm init -y in an empty folder. This creates package.json with defaults.
Step 2: Install Express
Run npm install express. This adds Express to your dependencies.
Step 3: Install Dev Dependencies
Run npm install --save-dev nodemon. nodemon restarts the server on file changes. Add a dev script: "dev": "nodemon src/server.js".
Step 4: Create the Server File
Create src/server.js:
const express = require('express');
const app = express();
app.get('/health', (req, res) => {
res.status(200).json({ status: 'ok' });
});
const PORT = process.env.PORT || 3000;
app.listen(PORT, () => {
console.log(`Server running on port ${PORT}`);
});
Step 5: Add JSON Body Parsing
Add app.use(express.json()) before your routes. This lets you read JSON bodies from POST requests. Without it, req.body is undefined.
Step 6: Add Cor
Run npm install cors and add app.use(cors()) if your client and server are on different origins. Skip this if same origin.
Step 7: Add Environment Variables
Run npm install dotenv. Create a .env file with PORT=3000. Add require('dotenv').config() at the top of server.js. Add .env to .gitignore.
Step 8: Run the Server
Run npm run dev. Visit http://localhost:3000/health. You should see { "status": "ok" }.
Step 9: Add a Health Check
You already added /health. Keep it. It is useful for uptime monitors, load balancers, and CI checks.
Step 10: Add an Error Handler
At the bottom of server.js (after all routes), add an error-handling middleware:
app.use((err, req, res, next) => {
console.error(err.stack);
res.status(500).json({ error: 'Something went wrong' });
});
The Takeaway
Create an Express server by initializing the project, installing Express and nodemon, creating server.js with a health check route, adding JSON body parsing, CORS (if cross-origin), dotenv for env vars, and an error-handling middleware. Run with npm run dev and confirm /health returns 200.
Run npm init -y, install express and nodemon, create server.js with app.get for health, add express.json(), cors if cross-origin, dotenv for env vars, and an error handler. Run with npm run dev and visit /health to confirm.
To read JSON request bodies. Without it, req.body is undefined for JSON POST requests. Add it before your routes.
It restarts the server automatically when files change. Without it, you would have to stop and start the server manually every time you save a file. Add it as a dev dependency and use it in your dev script.
In an environment variable. Use dotenv to load a .env file with PORT=3000. In code, read process.env.PORT || 3000. Never hardcode configuration values.
GET /health returning 200 OK is useful for uptime monitors, load balancers, and CI checks. It confirms the server is alive and the port is open. Add it on day one.
Ready to master Node.js completely?
Want to upskill yourself, crack your next interview, and get your dream job? Join our comprehensive course to dive deeper with high-quality video tutorials, solve interview questions, and a premium community.
Master Node.js
Want to upskill yourself, crack your next interview, and get your dream job? Join our comprehensive course.

