Back to Modules

Complete Backend Development Tutorial

Master backend development with our comprehensive guide covering Node.js, Express.js, and MongoDB. Learn through practical examples and build production-ready applications.

Part 1

Part 2

Project Initialization & Package Management

Learn how to initialize a Node.js project, manage dependencies, and configure package.json for modern JavaScript (ES Modules).

Step 1: Initialize Node.js Project

$ npm init -y

Creates a package.json file with default values

Step 2: package.json Configuration

{
  "name": "backend-project",
  "version": "1.0.0",
  "description": "Complete backend application",
  "main": "server.js",
  "type": "module",
  "scripts": {
    "start": "node server.js",
    "dev": "nodemon server.js"
  },
  "keywords": ["backend", "express", "mongodb"],
  "author": "Your Name",
  "license": "ISC",
  "dependencies": {
    "express": "^4.18.2",
    "mongoose": "^7.0.0",
    "dotenv": "^16.0.3",
    "cors": "^2.8.5",
    "jsonwebtoken": "^9.0.0"
  },
  "devDependencies": {
    "nodemon": "^2.0.22"
  }
}

✓ "type": "module" enables ES6 import/export syntax

Step 3: Install Dependencies

$ npm install express mongoose dotenv cors jsonwebtoken

Installs packages and creates node_modules folder and package-lock.json

Step 4: Install Dev Dependencies

$ npm install --save-dev nodemon

Nodemon auto-restarts server on file changes during development

Step 5: Recommended Project Structure

backend-project/
├── node_modules/
├── models/
│   └── User.js
├── routes/
│   └── userRoutes.js
├── middleware/
│   └── auth.js
├── .env
├── .gitignore
├── package.json
├── package-lock.json
└── server.js

Organized folder structure for scalable backend applications

Fetching Data from Real API - GET Request

Learn how to make GET requests to a real API and handle responses. We'll use the FreeAPI service to fetch user data.

Examples:

// Fetching data from API
const url = 'https://api.freeapi.app/api/v1/public/randomusers?page=1&limit=10';
const options = {
  method: 'GET',
  headers: {
    accept: 'application/json'
  }
};

async function fetchUsers() {
  const response = await fetch(url, options);
  const data = await response.json();
  return data;
}

fetchUsers();

Understanding API request structure and response format from FreeAPI random users endpoint

Fetching Random Products - API with Pagination

Learn how to fetch products from an API with pagination parameters and handle the response data structure.

Examples:

// Fetching products from API
const url = 'https://api.freeapi.app/api/v1/public/randomproducts?page=1&limit=10&query=mens-watches';
const options = {
  method: 'GET',
  headers: {
    accept: 'application/json'
  }
};

async function fetchProducts() {
  const response = await fetch(url, options);
  const data = await response.json();
  return data;
}

fetchProducts();

Understanding products API with query parameters and pagination from FreeAPI

Creating Your Own Simple API

Learn how to create a basic REST API with Express.js. This example shows the fundamental concepts of building API endpoints.

Examples:

// Creating a Simple REST API with Express
const express = require('express');
const app = express();

app.use(express.json());

// In-memory database
const books = [
  { id: 1, title: 'JavaScript Basics', author: 'John Doe', year: 2020 },
  { id: 2, title: 'Node.js Guide', author: 'Jane Smith', year: 2021 },
  { id: 3, title: 'React Mastery', author: 'Bob Johnson', year: 2022 }
];

// GET all books
app.get('/api/books', (req, res) => {
  res.json({ success: true, data: books });
});

// GET single book
app.get('/api/books/:id', (req, res) => {
  const book = books.find(b => b.id === parseInt(req.params.id));
  res.json({ success: true, data: book });
});

// POST new book
app.post('/api/books', (req, res) => {
  const newBook = { id: books.length + 1, ...req.body };
  books.push(newBook);
  res.status(201).json({ success: true, message: 'Book created', data: newBook });
});

app.listen(3000, () => {
  console.log('Server running on port 3000');
});

Step-by-step guide to creating a simple REST API with Express.js