A complete web application template with user authentication features built with Express.js, PostgreSQL, and vanilla JavaScript. This template serves as a foundation for students to build upon in their web development projects.
- User registration and login system
- Secure password hashing with bcrypt
- Session management with express-session
- Protected routes that require authentication
- CSRF protection
- Form validation with express-validator
- Responsive UI with clean CSS (no frameworks)
- MVC architecture pattern with EJS templating
- PostgreSQL database integration
- Profile image upload and storage
.
├── app.js # Application entry point
├── config/ # Configuration files
│ └── database.js # PostgreSQL connection pool
├── controllers/ # Route controllers
│ ├── authController.js # Authentication logic
│ └── userController.js # User-related logic
├── middlewares/ # Custom middleware
│ ├── auth.js # Authentication middleware
│ ├── error-handler.js # Error handling middleware
│ ├── locals.js # Template locals middleware
│ └── upload.js # File upload middleware
├── models/ # Database operations
│ ├── User.js # User data access functions
│ └── Image.js # Profile image data access
├── public/ # Static assets
│ ├── css/
│ ├── js/
│ └── images/
├── routes/ # Express routes
│ ├── auth.js # Authentication routes
│ ├── index.js # Public routes
│ └── user.js # Protected user routes
├── scripts/ # Utility scripts
│ └── init-db.js # Database initialization
└── views/ # EJS templates
├── partials/ # Reusable template parts (header, footer, etc.)
├── auth/ # Authentication templates
└── user/ # User-related templates
- Node.js (v18 or higher)
- PostgreSQL
-
Fork and clone the repository:
# Fork on GitHub first, then clone your fork git clone https://github.com/YOUR-USERNAME/CSC317Project-F25.git cd CSC317Project-F25
-
Install dependencies:
npm install -
Create a
.envfile in the root directory:cp .env.example .env -
Modify the
.envfile with your configuration:PORT=3000 NODE_ENV=development DATABASE_URL=postgresql://postgres:your_password@localhost:5432/csc317_project SESSION_SECRET=your_secure_secret_key -
Create the database and initialize tables:
createdb csc317_project npm run db:init -
Start the development server:
npm run dev -
Open your browser and visit
http://localhost:3000
This template includes detailed comments throughout the codebase to explain:
- Authentication flow and best practices
- Password hashing implementation
- Session management
- Middleware usage
- Security considerations
- Form validation
- SQL queries and database interactions
- Passwords are hashed using bcrypt
- CSRF protection for all forms
- HTTP-only session cookies
- Input validation and sanitization
- Parameterized SQL queries (prevents SQL injection)
- Secure session configuration
- Protection against common web vulnerabilities
npm run dev
This will start the server with nodemon, which automatically restarts when files change.
npm start
For deployment to Render.com:
- Create a PostgreSQL database on Render
- Create a Web Service connected to your GitHub repository
- Set environment variables (DATABASE_URL, SESSION_SECRET, NODE_ENV)
- Initialize the database using Render Shell:
npm run db:init
See SETUP.md for detailed deployment instructions.
CREATE TABLE users (
id SERIAL PRIMARY KEY,
username VARCHAR(20) NOT NULL UNIQUE,
email VARCHAR(255) NOT NULL UNIQUE,
password VARCHAR(255) NOT NULL,
has_profile_image BOOLEAN DEFAULT FALSE,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);CREATE TABLE profile_images (
id SERIAL PRIMARY KEY,
user_id INTEGER NOT NULL UNIQUE REFERENCES users(id) ON DELETE CASCADE,
data BYTEA NOT NULL,
content_type VARCHAR(50) NOT NULL,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);This project is available for educational purposes.
- This template was created as a learning resource for web development students
- Built with Express.js, PostgreSQL, and other open-source technologies