Skip to content

A simple forum web application built with Node.js, Express.js, and MongoDB. This is a beginner-level project demonstrating basic web development concepts including user authentication, CRUD operations, and session management.

Notifications You must be signed in to change notification settings

souilimaa/Forum

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

2 Commits
Β 
Β 

Repository files navigation

Forum Application

A simple forum web application built with Node.js, Express.js, and MongoDB. This is a beginner-level project demonstrating basic web development concepts including user authentication, CRUD operations, and session management.

πŸš€ Features

  • User Authentication

    • User registration with email validation
    • Secure login/logout functionality
    • Password hashing with bcrypt
    • Session management
  • Forum Functionality

    • Post new questions
    • View all questions from other users
    • Response system for questions
    • User-specific content management
  • Security

    • Password hashing
    • Session-based authentication
    • Input validation
    • SQL injection protection through MongoDB

πŸ›  Technologies Used

Backend

  • Node.js - Runtime environment
  • Express.js - Web framework
  • MongoDB - NoSQL database
  • Mongoose - MongoDB object modeling
  • bcrypt - Password hashing
  • express-session - Session management
  • body-parser - Request body parsing

Frontend

  • Pug - Template engine
  • CSS3 - Styling
  • HTML5 - Structure

Development Tools

  • npm - Package manager
  • nodemon - Development server (optional)

πŸ“ Project Structure

Forum/
β”œβ”€β”€ app.js                 # Main application file
β”œβ”€β”€ package.json          # Project dependencies
β”œβ”€β”€ controllers/          # Business logic
β”‚   β”œβ”€β”€ questionController.js
β”‚   β”œβ”€β”€ responseController.js
β”‚   └── userController.js
β”œβ”€β”€ models/               # Database models
β”‚   β”œβ”€β”€ User.js
β”‚   β”œβ”€β”€ Question.js
β”‚   └── Response.js
β”œβ”€β”€ routes/               # Application routes
β”‚   β”œβ”€β”€ usersRouter.js
β”‚   β”œβ”€β”€ questionRouter.js
β”‚   └── responseRouter.js
β”œβ”€β”€ middlewares/          # Custom middleware
β”‚   └── auth.js
β”œβ”€β”€ views/                # Pug templates
β”‚   β”œβ”€β”€ home.pug
β”‚   β”œβ”€β”€ login.pug
β”‚   β”œβ”€β”€ register.pug
β”‚   └── question.pug
└── public/               # Static files
    └── css/
        β”œβ”€β”€ homeStyle.css
        β”œβ”€β”€ loginStyle.css
        β”œβ”€β”€ questionStyle.css
        └── registerStyle.css

πŸ’Ύ Database Schema

User Model

{
  nom: String (required),
  email: String (required, unique),
  password: String (required)
}

Question Model

{
  user_id: ObjectId (ref: 'User', required),
  question: String (required),
  date: Date (default: Date.now)
}

Response Model

{
  user_id: ObjectId (ref: 'User', required),
  question_id: ObjectId (ref: 'Question', required),
  response: String (required),
  date: Date (default: Date.now)
}

βš™οΈ Installation & Setup

Prerequisites

  • Node.js (v14 or higher)
  • MongoDB (v4.0 or higher)
  • npm (comes with Node.js)

1. Clone the Repository

git clone https://github.com/souilimaa/Forum.git
cd Forum

2. Install Dependencies

npm install

3. Setup MongoDB

Option A: Local MongoDB Installation

  1. Install MongoDB on your system
  2. Start MongoDB service:
    # On Ubuntu/Debian
    sudo systemctl start mongod
    
    # On macOS (with Homebrew)
    brew services start mongodb-community
    
    # On Windows
    net start MongoDB

Option B: MongoDB Atlas (Cloud)

  1. Create a free account at MongoDB Atlas
  2. Create a new cluster
  3. Get your connection string
  4. Update app.js with your Atlas connection string:
    let connectionString = 'your-mongodb-atlas-connection-string';

4. Configure Database Connection

The application connects to MongoDB on localhost:27017 by default. Update the connection string in app.js if needed:

let connectionString = 'mongodb://localhost:27017';

5. Start the Application

node app.js

The application will be available at http://localhost:3000

πŸ–₯ Usage

  1. Access the Application: Open http://localhost:3000 in your web browser

  2. Register a New Account:

    • Click on "login" then "Register?"
    • Fill in your name, email, and password
    • Confirm your password
    • Click "Register"
  3. Login:

    • Enter your email and password
    • Click "Login"
  4. Post Questions:

    • On the home page, enter your question in the text field
    • Click "Envoyer" to submit
  5. View and Respond:

    • Click on "Reponses" next to any question to view or add responses
    • Add your response and submit
  6. Logout:

    • Click "Logout" in the top-right corner

🚧 API Endpoints

Authentication Routes

  • GET /login - Display login page
  • POST /home - Process login
  • GET /register - Display registration page
  • POST /users - Process registration
  • GET /logout - Logout user

Question Routes

  • POST /question - Create new question
  • GET /question/:id - View specific question with responses

Response Routes

  • POST /response - Add response to question

πŸ”§ Development

Running in Development Mode

For development with auto-reload, install nodemon:

npm install -g nodemon
nodemon app.js

Environment Variables

Create a .env file for environment-specific configurations:

PORT=3000
MONGODB_URI=mongodb://localhost:27017/forum
SESSION_SECRET=your-secret-key-here

πŸ› Troubleshooting

Common Issues

  1. MongoDB Connection Error

    Error: Connection error undefined
    

    Solution: Ensure MongoDB is running and accessible

  2. bcrypt Installation Issues

    Error: Module did not self-register
    

    Solution: Rebuild bcrypt module

    npm rebuild bcrypt
  3. Port Already in Use

    Error: listen EADDRINUSE: address already in use :::3000
    

    Solution: Kill the process using port 3000 or use a different port

    # Find and kill process using port 3000
    lsof -ti:3000 | xargs kill -9
  4. Session Issues

    • Clear browser cookies and cache
    • Restart the application

πŸ” Security Considerations

This is a beginner project and includes basic security measures:

Implemented Security Features

  • Password hashing with bcrypt
  • Session-based authentication
  • Input validation for forms
  • Protection against NoSQL injection via Mongoose

Recommended Enhancements for Production

  • Implement CSRF protection
  • Add rate limiting
  • Use HTTPS
  • Implement proper error handling
  • Add input sanitization
  • Use environment variables for sensitive data
  • Implement proper session configuration
  • Add logging and monitoring

πŸ‘€ Author

souilimaa

πŸ™ Acknowledgments

  • Built as a learning project for Node.js and Express.js
  • Inspired by basic forum applications
  • Uses MongoDB for data persistence
  • Styled with basic CSS for simplicity

πŸ“š Learning Resources

If you're new to the technologies used in this project:


Note: This is a beginner-level project created for educational purposes. It demonstrates basic web development concepts and should be enhanced with additional security measures before production use.

About

A simple forum web application built with Node.js, Express.js, and MongoDB. This is a beginner-level project demonstrating basic web development concepts including user authentication, CRUD operations, and session management.

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published