Skip to main content

Backend overview

The backend is a Node.js application built with Express 5.1.0, providing a lightweight API server. The server is designed to handle HTTP requests with CORS support and runs on a configurable port.

Server structure

The backend code is located in the server/ directory:
server/
├── index.js         # Main server file
├── package.json     # Dependencies
└── node_modules/    # Installed packages

Dependencies

The backend uses minimal dependencies from server/package.json:
{
  "dependencies": {
    "cors": "^2.8.5",
    "express": "^5.1.0"
  }
}
  • express - Fast, unopinionated web framework for Node.js
  • cors - Middleware for enabling Cross-Origin Resource Sharing
The lightweight dependency footprint makes the application fast to install and deploy.

Express server setup

The complete server implementation is in server/index.js:
const express = require('express');
const cors = require('cors');
const app = express();

app.use(cors());

app.get('/', (req, res) => {
  res.send('Hello, This is from Mini project Deployment!');
});

const PORT = process.env.PORT || 5000;
app.listen(PORT, () => console.log(`✅ Server running on port ${PORT}`));

Server initialization

The server follows these steps:
1

Import dependencies

Loads Express framework and CORS middleware
2

Create app instance

Initializes an Express application with express()
3

Configure middleware

Applies CORS middleware to all routes
4

Define routes

Sets up API endpoints (currently one GET route)
5

Start server

Listens on the configured port

CORS configuration

The server enables Cross-Origin Resource Sharing with the default CORS middleware:
app.use(cors());
This configuration:
  • Allows requests from any origin
  • Enables all CORS headers
  • Permits all HTTP methods
In production, you should restrict CORS to specific origins:
app.use(cors({
  origin: 'https://yourdomain.com',
  methods: ['GET', 'POST'],
  credentials: true
}));

API endpoints

The server currently implements one endpoint:

GET /

Returns a welcome message from the server. Implementation:
app.get('/', (req, res) => {
  res.send('Hello, This is from Mini project Deployment!');
});
Response:
Hello, This is from Mini project Deployment!
Example request:
curl http://localhost:5000/
This is a basic endpoint for testing server connectivity. In a production application, you would add additional routes for your API functionality.

Port configuration

The server port is configurable through an environment variable:
const PORT = process.env.PORT || 5000;
app.listen(PORT, () => console.log(`✅ Server running on port ${PORT}`));

How it works

  • Checks for the PORT environment variable
  • Falls back to port 5000 if not set
  • Logs a confirmation message when the server starts

Setting the port

export PORT=3000
node server/index.js

Running the server

Development mode

cd server
npm install
node index.js
The server will start and output:
✅ Server running on port 5000

Production mode (Docker)

In the Docker container, the server starts automatically:
CMD ["node", "server/index.js"]
The Dockerfile exposes port 5000:
EXPOSE 5000

Server architecture details

Request flow

  1. Client sends HTTP request to the server
  2. CORS middleware processes the request headers
  3. Express routes the request to the appropriate handler
  4. Handler processes the request and generates a response
  5. Response is sent back to the client

Middleware stack

The current middleware stack is minimal:
1

CORS middleware

Handles cross-origin requests by setting appropriate headers
2

Route handlers

Process requests and generate responses
For a production application, consider adding:
  • express.json() for parsing JSON request bodies
  • express.static() to serve the React frontend
  • helmet() for security headers
  • morgan() for request logging
  • Rate limiting middleware

Extending the backend

To add more functionality, you can expand the server with additional routes:
const express = require('express');
const cors = require('cors');
const app = express();

// Middleware
app.use(cors());
app.use(express.json());

// Serve static files (React frontend)
app.use(express.static('client/build'));

// API routes
app.get('/api/health', (req, res) => {
  res.json({ status: 'healthy', timestamp: new Date() });
});

app.get('/api/data', (req, res) => {
  res.json({ message: 'Your data here' });
});

// Fallback to React app
app.get('*', (req, res) => {
  res.sendFile(__dirname + '/client/build/index.html');
});

const PORT = process.env.PORT || 5000;
app.listen(PORT, () => console.log(`✅ Server running on port ${PORT}`));

Key features

Lightweight

Minimal dependencies for fast startup and deployment

CORS enabled

Ready for cross-origin requests from frontend apps

Configurable port

Adapts to different deployment environments

Express 5.x

Uses the latest major version with improved performance

Production considerations

Store sensitive configuration in environment variables, not in code:
  • Database URLs
  • API keys
  • Session secrets
Add global error handling middleware:
app.use((err, req, res, next) => {
  console.error(err.stack);
  res.status(500).send('Something broke!');
});
Implement structured logging for production monitoring:
  • Request logs
  • Error logs
  • Performance metrics
Enhance security with additional middleware:
  • helmet for security headers
  • rate limiting to prevent abuse
  • input validation

Next steps

System overview

Understand how frontend and backend work together

Frontend architecture

Learn about the React application structure

Build docs developers (and LLMs) love