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.
- Overview
- Features
- Project Architecture
- Installation
- Usage
- API Endpoints
- Code Analysis
- Module System
- Technologies Used
- Learning Objectives
- Development Scripts
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.
- π 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
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
- Clone or download the project
- Navigate to the project directory
cd "Node Farm"
- Install dependencies
npm install
npm run devnpm startnode index.jsAccess 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
| 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 |
The application follows a modular architecture with clear separation of concerns:
- π― Main Server (
index.js): Request handling and routing - π Data Layer (
Modules/dataLoader.js): Data management - π¨ Template Engine (
Modules/templateEngine.js): HTML generation - π Template Loader (
Modules/templateLoader.js): Template file management
const server = http.createServer((req, res) => {
const { pathname, query } = url.parse(req.url, true);
// Route handling logic
});Key Features:
- Uses Node.js built-in
httpmodule - Listens on port 5000 with IP 127.0.0.1
- URL parsing with query parameter support
- Comprehensive error handling
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..."
}Core Functions:
createProductCard(template, product): Individual product card generationcreateProductCards(): All product cards compilationcreateOverviewTemplate(): Complete overview page assemblycreateProductTemplate(product): Individual product page generation
Template Processing:
- Uses placeholder replacement (
{{placeholder}}) - Supports global replacements with regex patterns
- Dynamic content injection
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 containerproductCard.html: Reusable product card componentproduct.html: Detailed product page layout
Modern CSS Features:
- π Gradient Backgrounds: Beautiful green gradients (
#9be15dto#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
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 |
- π 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
index.js
βββ Modules/dataLoader.js
βββ Modules/templateEngine.js
β βββ Modules/templateLoader.js
β βββ Modules/dataLoader.js
βββ Modules/templateLoader.js
- Loads and parses JSON data
- Provides centralized data access
- Handles data format conversion
- Template processing and rendering
- Placeholder replacement logic
- Dynamic content generation
- Template file management
- Synchronous file reading
- Template caching
- Node.js: Runtime environment
- HTTP Module: Web server creation
- File System (fs): File operations
- URL Module: URL parsing and routing
- HTML5: Semantic markup structure
- CSS3: Modern styling and responsive design
- JavaScript: Dynamic content manipulation
- JSON: Data storage and configuration
- NPM: Package management
- Nodemon: Development server with auto-reload
- Slugify: URL slug generation (available but not used in current code)
- Package.json: Project configuration and scripts
This project demonstrates essential Node.js concepts:
- π HTTP Server Creation: Building web servers from scratch
- π File System Operations: Reading files synchronously
- π URL Routing: Handling different endpoints and query parameters
- π¨ Template Rendering: Dynamic HTML generation
- ποΈ JSON Data Handling: Working with structured data
β οΈ Error Handling: Managing invalid requests and 404 errors
- π§© Modular Design: Code organization and separation of concerns
- π Code Reusability: Creating reusable components
- π¦ Module System: Node.js CommonJS modules
- π Template Systems: Dynamic content generation
- π± Responsive Design: Mobile-first CSS approach
- π¨ Modern CSS: Gradients, transitions, and animations
- π RESTful APIs: JSON endpoint creation
- π§ͺ Development Workflow: Scripts and automation
{
"scripts": {
"dev": "npx nodemon index.js", // Development with auto-reload
"start": "node index.js" // Production start
}
}{
"dependencies": {
"slugify": "^1.6.6" // URL slug generation
},
"devDependencies": {
"nodemon": "^3.1.10" // Development server
}
}- π± 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
- π Intuitive Navigation: Clear routing structure
- β‘ Fast Loading: Efficient template rendering
- οΏ½ Mobile-friendly: Responsive design
- π¨ Consistent UI: Unified design system
- β Prerequisites: Ensure Node.js is installed
- π₯ Installation: Run
npm install - π Development: Run
npm run dev - π Access: Open
http://127.0.0.1:5000 - π Explore: Navigate through the product catalog!
- Use
npm run devfor development with auto-reload - Check console for server status and errors
- Modify
data.jsonto add/update products - Edit templates in
templates/folder for UI changes
This project is licensed under the ISC License.
Ahmed Esam
- GitHub: @AhmedEsam2002
- Repository: Node-Farm
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.