Skip to content

AhmedEsam2002/Node-Farm

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

3 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

🌽 Node Farm πŸ₯¦

A modern, elegant Node.js web application showcasing fresh organic produce with a beautiful, responsive design. This project demonstrates fundamental Node.js concepts including HTTP server creation, file system operations, URL routing, modular architecture, and template rendering with a clean separation of concerns.

πŸ“‹ Table of Contents

🌟 Overview

Node Farm is a full-stack web application built with pure Node.js that showcases an organic produce marketplace. The project emphasizes clean architecture, modular design, and fundamental web development concepts without external frameworks.

✨ Features

  • 🏠 Product Overview Page: Displays all available fresh produce in an attractive card layout
  • πŸ“‹ Individual Product Pages: Detailed view of each product with comprehensive information
  • πŸ”Œ JSON API: RESTful API endpoint for product data
  • πŸ“± Responsive Design: Beautiful CSS styling with gradient backgrounds and hover effects
  • 🎨 Template System: Dynamic HTML generation using placeholder replacement
  • 🌱 Organic Product Highlighting: Special styling for organic products
  • ⚑ Error Handling: Custom 404 page for invalid routes
  • πŸ”„ Hot Reload: Development server with nodemon for automatic restarts
  • πŸ“¦ Modular Architecture: Clean separation of concerns with organized modules

πŸ—οΈ Project Architecture

Node Farm/
β”œβ”€β”€ index.js                 # Main server file & routing logic
β”œβ”€β”€ package.json            # Project configuration & dependencies
β”œβ”€β”€ data.json              # Product database (JSON)
β”œβ”€β”€ README.md              # Project documentation
β”œβ”€β”€ Modules/               # Core application modules
β”‚   β”œβ”€β”€ dataLoader.js      # Data loading & parsing utilities
β”‚   β”œβ”€β”€ templateEngine.js  # Template rendering engine
β”‚   └── templateLoader.js  # Template file loading utilities
└── templates/             # HTML template files
    β”œβ”€β”€ overview.html      # Main page template
    β”œβ”€β”€ product.html       # Individual product page template
    └── productCard.html   # Product card component template

πŸš€ Installation

  1. Clone or download the project
  2. Navigate to the project directory
    cd "Node Farm"
  3. Install dependencies
    npm install

πŸ’» Usage

Development Mode (with auto-reload)

npm run dev

Production Mode

npm start

Manual Start

node index.js

Access the application:

  • Open your browser and go to: http://127.0.0.1:5000
  • Or use: http://localhost:5000

Navigate the application:

  • 🏠 Home/Overview: / or /overview - View all products
  • πŸ“‹ Product Details: /product?id=0 - View specific product (replace 0 with product ID)
  • πŸ”Œ API Access: /api - Get JSON data of all products

πŸ”Œ API Endpoints

Endpoint Method Description Response Type Status Code
/ or /overview GET Homepage with all products HTML 200
/product?id={id} GET Individual product page HTML 200/404
/api GET All products data JSON 200
* GET 404 Not Found page HTML 404

πŸ” Code Analysis

πŸ—οΈ Architecture Overview

The application follows a modular architecture with clear separation of concerns:

  1. 🎯 Main Server (index.js): Request handling and routing
  2. πŸ“ Data Layer (Modules/dataLoader.js): Data management
  3. 🎨 Template Engine (Modules/templateEngine.js): HTML generation
  4. πŸ“„ Template Loader (Modules/templateLoader.js): Template file management

πŸ“Š Core Components

1. πŸš€ Server Setup (index.js)

const server = http.createServer((req, res) => {
  const { pathname, query } = url.parse(req.url, true);
  // Route handling logic
});

Key Features:

  • Uses Node.js built-in http module
  • Listens on port 5000 with IP 127.0.0.1
  • URL parsing with query parameter support
  • Comprehensive error handling

2. πŸ—„οΈ Data Management (Modules/dataLoader.js)

const products = JSON.parse(fs.readFileSync("./data.json", "utf-8"));

Product Schema:

{
  "id": 0,
  "productName": "Fresh Avocados",
  "image": "πŸ₯‘",
  "from": "Spain",
  "nutrients": "Vitamin B, Vitamin K",
  "quantity": "4 πŸ₯‘",
  "price": "6.50",
  "organic": true,
  "description": "Detailed product description..."
}

3. 🎨 Template System (Modules/templateEngine.js)

Core Functions:

  • createProductCard(template, product): Individual product card generation
  • createProductCards(): All product cards compilation
  • createOverviewTemplate(): Complete overview page assembly
  • createProductTemplate(product): Individual product page generation

Template Processing:

  • Uses placeholder replacement ({{placeholder}})
  • Supports global replacements with regex patterns
  • Dynamic content injection

4. πŸ“„ Template Loader (Modules/templateLoader.js)

const overviewTemplate = fs.readFileSync("./templates/overview.html", "utf-8");
const productCardTemplate = fs.readFileSync(
  "./templates/productCard.html",
  "utf-8"
);
const productTemplate = fs.readFileSync("./templates/product.html", "utf-8");

Template Files:

  • overview.html: Main layout with product cards container
  • productCard.html: Reusable product card component
  • product.html: Detailed product page layout

5. 🎨 Styling & Design

Modern CSS Features:

  • 🌈 Gradient Backgrounds: Beautiful green gradients (#9be15d to #00e3ae)
  • 🎭 Typography: Google Fonts (Megrim for headers, Nunito Sans for body)
  • πŸ“± Responsive Design: Flexbox and CSS Grid layouts
  • ✨ Visual Effects: Skewed elements, shadows, smooth transitions
  • 🎯 Interactive Elements: Hover effects and smooth animations
  • 🌿 Organic Theme: Green color scheme representing freshness

CSS Architecture:

  • Embedded styles in HTML templates
  • Consistent design system across all pages
  • Mobile-first responsive approach

6. πŸ“¦ Product Catalog

The application showcases 5 fresh produce items:

Product Emoji Origin Price Organic Nutrients
Fresh Avocados πŸ₯‘ Spain €6.50 βœ… Vitamin B, Vitamin K
Goat and Sheep Cheese πŸ§€ Portugal €5.00 ❌ Vitamin A, Calcium
Apollo Broccoli πŸ₯¦ Portugal €5.50 βœ… Vitamin C, Vitamin K
Baby Carrots πŸ₯• France €3.00 βœ… Vitamin A, Vitamin K
Sweet Corncobs 🌽 Germany €2.00 ❌ Vitamin C, Magnesium

🧩 Module System

πŸ“ Modular Architecture Benefits:

  • πŸ”„ Reusability: Components can be reused across the application
  • πŸ› οΈ Maintainability: Easy to update and debug individual modules
  • πŸ“ Scalability: Easy to add new features and modules
  • πŸ§ͺ Testability: Individual modules can be tested in isolation

πŸ”— Module Dependencies:

index.js
β”œβ”€β”€ Modules/dataLoader.js
β”œβ”€β”€ Modules/templateEngine.js
β”‚   β”œβ”€β”€ Modules/templateLoader.js
β”‚   └── Modules/dataLoader.js
└── Modules/templateLoader.js

πŸ“‹ Module Responsibilities:

πŸ—„οΈ dataLoader.js

  • Loads and parses JSON data
  • Provides centralized data access
  • Handles data format conversion

🎨 templateEngine.js

  • Template processing and rendering
  • Placeholder replacement logic
  • Dynamic content generation

πŸ“„ templateLoader.js

  • Template file management
  • Synchronous file reading
  • Template caching

πŸ›  Technologies Used

πŸ—οΈ Core Technologies:

  • Node.js: Runtime environment
  • HTTP Module: Web server creation
  • File System (fs): File operations
  • URL Module: URL parsing and routing

🎨 Frontend Technologies:

  • HTML5: Semantic markup structure
  • CSS3: Modern styling and responsive design
  • JavaScript: Dynamic content manipulation

πŸ“Š Data & Configuration:

  • JSON: Data storage and configuration
  • NPM: Package management
  • Nodemon: Development server with auto-reload

πŸ”§ Development Tools:

  • Slugify: URL slug generation (available but not used in current code)
  • Package.json: Project configuration and scripts

πŸ“š Learning Objectives

This project demonstrates essential Node.js concepts:

🎯 Core Concepts:

  1. πŸš€ HTTP Server Creation: Building web servers from scratch
  2. πŸ“ File System Operations: Reading files synchronously
  3. πŸ”€ URL Routing: Handling different endpoints and query parameters
  4. 🎨 Template Rendering: Dynamic HTML generation
  5. πŸ—„οΈ JSON Data Handling: Working with structured data
  6. ⚠️ Error Handling: Managing invalid requests and 404 errors

πŸ—οΈ Architecture Concepts:

  1. 🧩 Modular Design: Code organization and separation of concerns
  2. πŸ”„ Code Reusability: Creating reusable components
  3. πŸ“¦ Module System: Node.js CommonJS modules
  4. 🎭 Template Systems: Dynamic content generation

οΏ½ Web Development Concepts:

  1. πŸ“± Responsive Design: Mobile-first CSS approach
  2. 🎨 Modern CSS: Gradients, transitions, and animations
  3. πŸ”Œ RESTful APIs: JSON endpoint creation
  4. πŸ§ͺ Development Workflow: Scripts and automation

πŸ”§ Development Scripts

πŸ“‹ Available Scripts:

{
  "scripts": {
    "dev": "npx nodemon index.js", // Development with auto-reload
    "start": "node index.js" // Production start
  }
}

πŸ”§ Dependencies:

{
  "dependencies": {
    "slugify": "^1.6.6" // URL slug generation
  },
  "devDependencies": {
    "nodemon": "^3.1.10" // Development server
  }
}

🎨 Design Features

🌈 Visual Design:

  • 🌱 Nature-inspired: Green gradient backgrounds
  • 🎭 Modern Typography: Clean, readable fonts
  • πŸ“± Responsive Cards: Flexible product display
  • ✨ Smooth Animations: Polished user experience
  • 🏷️ Organic Badges: Special highlighting for organic products

🎯 User Experience:

  • πŸ”„ Intuitive Navigation: Clear routing structure
  • ⚑ Fast Loading: Efficient template rendering
  • οΏ½ Mobile-friendly: Responsive design
  • 🎨 Consistent UI: Unified design system

πŸš€ Getting Started

⚑ Quick Start:

  1. βœ… Prerequisites: Ensure Node.js is installed
  2. πŸ“₯ Installation: Run npm install
  3. πŸš€ Development: Run npm run dev
  4. 🌐 Access: Open http://127.0.0.1:5000
  5. πŸŽ‰ Explore: Navigate through the product catalog!

πŸ”§ Development Tips:

  • Use npm run dev for development with auto-reload
  • Check console for server status and errors
  • Modify data.json to add/update products
  • Edit templates in templates/ folder for UI changes

πŸ“„ License

This project is licensed under the ISC License.

πŸ‘¨β€πŸ’» Author

Ahmed Esam


This project is part of a Node.js learning course, demonstrating fundamental web development concepts with a focus on server-side JavaScript programming and modern web architecture.

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors