Skip to content

Conversation

@rlbisbe
Copy link
Owner

@rlbisbe rlbisbe commented Dec 3, 2025

Overview

This PR introduces a complete JavaScript/Express.js version of the phpblog application that replicates the entire architecture and functionality of the original PHP implementation. The new implementation maintains the same user experience while providing a modern Node.js backend.

Requirements Implemented

Core Application Setup

  • ✅ Express.js server setup with comprehensive routing
  • ✅ MySQL database integration using mysql2 client
  • ✅ Connection to MySQL database 'blog' with 'posts' table
  • ✅ EJS template engine for server-side rendering

Backend Functions (db.js)

Equivalent to backend.php functionality:

  • theTitle() - Function to fetch and display blog title
  • init(callback) - Function to initialize and fetch all posts from database
  • addNewPost(title, text, callback) - Function to add new posts with title and text fields

Frontend Routes

All routes matching the current PHP application:

  • GET / - Main blog page displaying all posts with login form
  • POST /login - Login route handling user authentication
  • GET /logout - Logout route clearing user session
  • GET /admin - Admin panel route to view all posts (protected)
  • GET /admin/new_post - Admin route to display new post form (protected)
  • POST /admin/add_new_post - Admin route to add new posts (protected)

Security & Authentication

  • ✅ Cookie-based authentication system using Express middleware
  • requireAuth middleware to protect admin routes
  • ✅ MD5 password hashing matching original PHP implementation
  • ✅ Session management via cookies

View Templates

All EJS templates replicating the existing PHP page layouts:

  • views/index.ejs - Main blog page
  • views/login.ejs - Login result page
  • views/logout.ejs - Logout confirmation
  • views/admin/index.ejs - Admin panel
  • views/admin/new_post.ejs - New post form
  • views/admin/add_new_post.ejs - Post creation result

File Structure

phpblog/
├── app.js                      # Main Express application with routes
├── db.js                       # Database layer (MySQL connection and queries)
├── package.json                # Node.js dependencies
├── README.md                   # Documentation for Express.js version
└── views/                      # EJS templates
    ├── index.ejs              # Main blog page
    ├── login.ejs              # Login result page
    ├── logout.ejs             # Logout confirmation page
    └── admin/
        ├── index.ejs          # Admin panel
        ├── new_post.ejs       # New post form
        └── add_new_post.ejs   # Post creation result

Technical Details

Dependencies

  • express ^4.18.2 - Web framework
  • ejs ^3.1.9 - Template engine
  • mysql2 ^3.6.5 - MySQL database client
  • cookie-parser ^1.4.6 - Cookie parsing middleware
  • body-parser ^1.20.2 - Form data parsing

Database Configuration

Matches original PHP configuration:

  • Host: localhost
  • User: root
  • Password: macbook
  • Database: blog
  • Table: posts (id, title, text)

Authentication Credentials

  • Username: roberto
  • Password: password (MD5: 3bc2e28ca8940090c3d80c851784a5d5)

Installation & Usage

# Install dependencies
npm install

# Start the server
npm start

# The application will run on http://localhost:3000

Testing Checklist

  • Server starts successfully on port 3000
  • Main page displays all posts from database
  • Login form accepts correct credentials
  • Login with correct credentials sets cookie and displays success
  • Login with incorrect credentials displays failure message
  • Logout clears session cookie
  • Admin routes redirect unauthenticated users
  • Admin panel displays all posts for authenticated users
  • New post form is accessible to authenticated users
  • Posts can be successfully added to database
  • New posts appear on main page after creation

Behavior Parity

All routes and functionality match the current PHP application behavior, ensuring:

  • Same authentication flow
  • Same database operations
  • Same page layouts and structure
  • Same user experience
  • Compatible with existing MySQL database

Notes

  • Original PHP files remain untouched for backwards compatibility
  • Both PHP and Express.js versions can coexist
  • Express.js version uses the same database and credentials as PHP version
  • All middleware and routing logic follows Express.js best practices

…ased authentication

Co-authored-by: Roberto Luis Bisbe <825331+rlbisbe@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants