Files
ToDo-App_Node.js_Test/README.md
2025-04-06 15:53:08 +02:00

3.7 KiB

Todo App with Node.js, Express, and PostgreSQL

A full-stack Todo application with user authentication built using Node.js, Express, PostgreSQL, and JWT.

Features

  • User authentication (register, login, logout)
  • JWT-based session management with HTTP-only cookies
  • Create, read, update, and delete todo items
  • Responsive UI with real-time updates
  • PostgreSQL database storage
  • Docker support for development

Technologies Used

  • Backend: Node.js, Express.js
  • Database: PostgreSQL
  • Authentication: JWT (JSON Web Tokens), bcrypt for password hashing
  • Frontend: HTML, CSS, JavaScript (Vanilla)
  • Development Tools: Docker, Nodemon

Setup Instructions

Prerequisites

  • Node.js (v14 or newer)
  • npm or yarn
  • Docker and Docker Compose (for containerized database)
  • PostgreSQL (if not using Docker)

Installation

  1. Clone the repository:

    git clone https://git.mahom03-spacecloud.de/CodeDevMLH/ToDo-App_Node.js_Test.git
    cd ToDo-App_Node.js_Test
    
  2. Install dependencies:

    npm install
    

Database Setup

Using Docker:

  1. Start PostgreSQL container:

    npm run create-db-container
    
  2. Create the database:

    npm run db:create
    
  3. Initialize the database with tables:

    npm run db:init
    

Manual Setup:

  1. Create a PostgreSQL database named todo_app_db
  2. Run the SQL script in /db/init_postgres.sql to create tables

Environment Configuration

Create a .env file in the root directory with the following variables:

# PostgreSQL Database Configuration
DB_USER=postgres
DB_HOST=localhost
DB_DATABASE=todo_app_db
DB_PASSWORD=password
DB_PORT=5432

# JWT Configuration
JWT_SECRET=your_secure_secret_key
JWT_EXPIRES_IN=1h

# Server Configuration
PORT=3000

Important

Replace your_secure_secret_key with a strong, random secret for production use.

Running the Application

Development mode with auto-restart:

npm run dev

Production mode:

npm start

Access the application at: http://localhost:3000

API Endpoints

Authentication

  • POST /api/auth/register - Register a new user
  • POST /api/auth/login - Log in a user
  • POST /api/auth/logout - Log out the current user
  • GET /api/auth/status - Check user's authentication status

Todo Items

  • GET /api/todos - Get all todos for logged-in user
  • POST /api/todos/newEntry - Create a new todo
  • PUT /api/todos/:id - Update a todo's completion status
  • DELETE /api/todos/:id - Delete a todo

Project Structure

.
├── db/
│   └── init_postgres.sql    # Database initialization script
├── middleware/
│   └── authMiddleware.js    # JWT authentication middleware
├── public/                  # Static frontend files
│   ├── index.html           # Main todo app page
│   ├── login.html           # Login page
│   ├── register.html        # Registration page
│   ├── script.js            # Frontend JavaScript
│   └── style.css            # CSS styles
├── routes/
│   ├── authRoutes.js        # Authentication routes
│   ├── todoRoutes.js        # Todo CRUD operations
│   └── viewRoutes.js        # HTML page serving
├── .env                     # Environment variables
├── .gitignore               # Git ignore file
├── db.js                    # Database connection setup
├── package.json             # Project dependencies
├── README.md                # Project documentation
└── server.js                # Main application entry point

This project was created for learning and demonstration purposes only.