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.
-
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
- 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
- Pug - Template engine
- CSS3 - Styling
- HTML5 - Structure
- npm - Package manager
- nodemon - Development server (optional)
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
{
nom: String (required),
email: String (required, unique),
password: String (required)
}{
user_id: ObjectId (ref: 'User', required),
question: String (required),
date: Date (default: Date.now)
}{
user_id: ObjectId (ref: 'User', required),
question_id: ObjectId (ref: 'Question', required),
response: String (required),
date: Date (default: Date.now)
}- Node.js (v14 or higher)
- MongoDB (v4.0 or higher)
- npm (comes with Node.js)
git clone https://github.com/souilimaa/Forum.git
cd Forumnpm install- Install MongoDB on your system
- Start MongoDB service:
# On Ubuntu/Debian sudo systemctl start mongod # On macOS (with Homebrew) brew services start mongodb-community # On Windows net start MongoDB
- Create a free account at MongoDB Atlas
- Create a new cluster
- Get your connection string
- Update
app.jswith your Atlas connection string:let connectionString = 'your-mongodb-atlas-connection-string';
The application connects to MongoDB on localhost:27017 by default. Update the connection string in app.js if needed:
let connectionString = 'mongodb://localhost:27017';node app.jsThe application will be available at http://localhost:3000
-
Access the Application: Open
http://localhost:3000in your web browser -
Register a New Account:
- Click on "login" then "Register?"
- Fill in your name, email, and password
- Confirm your password
- Click "Register"
-
Login:
- Enter your email and password
- Click "Login"
-
Post Questions:
- On the home page, enter your question in the text field
- Click "Envoyer" to submit
-
View and Respond:
- Click on "Reponses" next to any question to view or add responses
- Add your response and submit
-
Logout:
- Click "Logout" in the top-right corner
GET /login- Display login pagePOST /home- Process loginGET /register- Display registration pagePOST /users- Process registrationGET /logout- Logout user
POST /question- Create new questionGET /question/:id- View specific question with responses
POST /response- Add response to question
For development with auto-reload, install nodemon:
npm install -g nodemon
nodemon app.jsCreate a .env file for environment-specific configurations:
PORT=3000
MONGODB_URI=mongodb://localhost:27017/forum
SESSION_SECRET=your-secret-key-here-
MongoDB Connection Error
Error: Connection error undefinedSolution: Ensure MongoDB is running and accessible
-
bcrypt Installation Issues
Error: Module did not self-registerSolution: Rebuild bcrypt module
npm rebuild bcrypt
-
Port Already in Use
Error: listen EADDRINUSE: address already in use :::3000Solution: Kill the process using port 3000 or use a different port
# Find and kill process using port 3000 lsof -ti:3000 | xargs kill -9
-
Session Issues
- Clear browser cookies and cache
- Restart the application
This is a beginner project and includes basic security measures:
- Password hashing with bcrypt
- Session-based authentication
- Input validation for forms
- Protection against NoSQL injection via Mongoose
- 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
souilimaa
- GitHub: @souilimaa
- 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
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.