This is my adaptation of the Codecademy Project - see here. The Project's requirements are documented here.
A full-stack web app that hosts a collection of images. Authenticated users can submit creative captions. It also features:
- user authentication
- session management
- RESTful API endpoints
- interactive web views
- database persistence with PostgreSQL
- performance optimization through localized caching
erDiagram
User ||--o{ Caption : "submits"
Image ||--o{ Caption : "has"
User {
int id PK
string userName "unique"
string email "unique"
string password "hashed"
}
Image {
int id PK
string url
}
Caption {
int id PK
string text
int userId FK
int imageId FK
}
| Method | Endpoint | Auth Required | Description |
|---|---|---|---|
| GET | /api/images |
No | Retrieve all images with captions and user data |
| GET | /api/images/:id |
No | Get specific image with all captions |
| POST | /api/images/:id/captions |
Yes | Submit a caption for an image |
| Method | Route | Description |
|---|---|---|
| GET | / |
Home page with image gallery |
| GET | /images/:id |
Single image detail page |
| POST | /images/:id/captions |
Submit caption (HTML form) |
| GET | /register |
Registration form |
| POST | /register |
Process registration |
| GET | /login |
Login form |
| POST | /login |
Process login |
| POST | /logout |
End session |
| Route | Description |
|---|---|
/api-docs |
Interactive Swagger UI with full API documentation |
- Node.js (v16+)
- PostgreSQL (v12+)
- npm
-
Clone the repository
-
Install dependencies
npm install
-
Configure environment variables
Create a
.envfile in the project rootUSERNAME=your_postgres_username PASSWORD=your_postgres_password DATABASE=your_database_name SESSION_SECRET=your_random_secret_key
-
Create PostgreSQL database
psql -U postgres CREATE DATABASE your_database_name; -
Build the application
npm run build
-
Start the server
npm start
For development with auto-recompilation:
npm run dev:watch
-
Access the application
- Web Interface:
http://localhost:3000 - API Documentation:
http://localhost:3000/api-docs
- Web Interface:
When you run npm start, the application executes the following initialization sequence:
- Loads environment variables from
.envfile - Configures TypeORM DataSource with PostgreSQL connection details
- Sets up EJS as the view engine with layout support
- Configures middleware stack:
- JSON and URL-encoded body parsing
- Static file serving from
/public - Session management with secure cookies
- Session data injection into view locals
- Mounts view routes at
/ - Mounts API routes at
/api - Mounts auth routes at
/(register, login, logout) - Serves Swagger documentation at
/api-docs
- Establishes connection to PostgreSQL via TypeORM
- Creates tables if they don't exist (synchronize: true)
- Initializes TypeORM repositories for User, Image, and Caption entities
- Checks if Image table is empty
- If empty, seeds 5 initial images from external URLs
- Skips seeding if images already exist
- Binds to port 3000 (or PORT environment variable)
- Logs confirmation message:
Server running on http://localhost:3000 - Application is ready to accept requests