Backend Development (Node.js, Django, Laravel)

What is Backend Development?

The backend is the engine of any modern web application. It runs on the server and is responsible for business logic, database operations, user authentication, and serving data to the frontend through APIs. Popular backend stacks include Node.js with Express, Python with Django, and PHP with Laravel โ€“ each offering a different philosophy and toolset.

In this guide, you'll learn the fundamentals of each stack with practical code examples you can run immediately.

Node.js & Express

1. A Simple Express Server

Express is a minimal and flexible Node.js web application framework. Start by installing Node.js, then run npm init -y and npm install express.

const express = require('express');
const app = express();
const port = 3000;

app.get('/', (req, res) => {
  res.send('Hello from the Node.js backend!');
});

app.listen(port, () => {
  console.log(`Server running at http://localhost:${port}`);
});

Run the server with node app.js and visit http://localhost:3000.

2. Routes & Middleware

Middleware functions have access to the request and response objects. They can modify the request, end the response, or call the next middleware.

// Builtโ€‘in middleware for parsing JSON bodies
app.use(express.json());

app.get('/api/users', (req, res) => {
  res.json([{ id: 1, name: 'Siraw' }]);
});

app.post('/api/users', (req, res) => {
  const newUser = req.body;
  // Save newUser to database...
  res.status(201).json(newUser);
});

3. Building a RESTful API

A complete CRUD (Create, Read, Update, Delete) example:

const users = [];

// Get all users
app.get('/users', (req, res) => res.json(users));

// Create a user
app.post('/users', (req, res) => {
  const user = req.body;
  users.push(user);
  res.status(201).json(user);
});

// Update a user
app.put('/users/:id', (req, res) => {
  const { id } = req.params;
  // Find and update...
  res.json({ message: `User ${id} updated` });
});

// Delete a user
app.delete('/users/:id', (req, res) => {
  const { id } = req.params;
  // Remove user...
  res.json({ message: `User ${id} deleted` });
});

Django (Python)

1. A Simple Django View

Django follows the MVT (Modelโ€‘Viewโ€‘Template) pattern. After creating a Django project and app, define a view in views.py.

from django.http import HttpResponse

def home(request):
    return HttpResponse("Hello from the Django backend!")

2. URL Configuration

Map the view to a URL pattern in urls.py.

from django.urls import path
from . import views

urlpatterns = [
    path('', views.home, name='home'),
]

3. Models & Admin Panel

Define a data model and register it to the builtโ€‘in admin interface.

from django.db import models

class Employee(models.Model):
    first_name = models.CharField(max_length=50)
    last_name = models.CharField(max_length=50)
    email = models.EmailField(unique=True)
    hire_date = models.DateField()

    def __str__(self):
        return f"{self.first_name} {self.last_name}"

# In admin.py
from django.contrib import admin
from .models import Employee

admin.site.register(Employee)

Laravel (PHP)

1. A Simple Route

Laravel routes are defined in routes/web.php. Here's the most basic route.

use Illuminate\Support\Facades\Route;

Route::get('/', function () {
    return 'Hello from the Laravel backend!';
});

2. Controller Example

Controllers group related request handling logic. Create a controller with php artisan make:controller UserController.

namespace App\Http\Controllers;

use App\Models\User;
use Illuminate\Http\Request;

class UserController extends Controller
{
    public function index()
    {
        return response()->json(User::all());
    }

    public function store(Request $request)
    {
        $user = User::create($request->all());
        return response()->json($user, 201);
    }
}

3. Eloquent ORM

Eloquent makes database interaction intuitive. Here are common queries.

// Define a model
class User extends Model {}

// Retrieve all users
$users = User::all();

// Filter users
$admins = User::where('role', 'admin')->get();

// Create a new user
User::create([
    'name' => 'Siraw',
    'email' => 'siraw@example.com',
]);