Skip to content

TheNeovimmer/wsanotes

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

9 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Notes API Application

A modern, RESTful API for managing notes with a clean and intuitive interface. This application provides a robust backend service for creating, reading, updating, and deleting notes with persistent storage using MySQL.

Features

  • RESTful API: Full CRUD (Create, Read, Update, Delete) operations for notes
  • MySQL Database: Persistent storage with automatic table creation
  • Swagger Documentation: Interactive API documentation at /api-docs
  • CORS Enabled: Cross-origin resource sharing for web applications
  • JSON Middleware: Built-in support for JSON request/response handling
  • Static File Serving: Serves static files from the public directory
  • Error Handling: Comprehensive error handling for database operations and API requests

Technology Stack

  • Backend: Node.js with Express.js framework
  • Database: MySQL
  • API Documentation: Swagger UI with OpenAPI 3.0 specification
  • Middleware:
    • CORS for cross-origin requests
    • Express JSON parser for request body handling
    • Express static file serving

Installation

Prerequisites

  • Node.js (v14 or higher)
  • MySQL database access
  • npm or yarn package manager

Setup

  1. Clone the repository

    git clone https://github.com/bouzayenilyes/wsanotes.git
    cd wsanotes
  2. Install dependencies

    npm install
  3. Database Configuration

    The application is configured to connect to a MySQL database. The connection details are already set in the main.js file:

    • Host: ...
    • User: ...
    • Password: ...
    • Database: ...
    • Port: 3307

    If you need to use your own database, update the connection details in the main.js file:

    const db = mysql.createConnection({
      host: "your-host",
      user: "your-username",
      password: "your-password",
      database: "your-database",
      port: 3306 // or your MySQL port
    });
  4. Run the application

    node main.js

    The server will start on port 3000 (or the port specified in the PORT environment variable).

API Documentation

Once the server is running, you can access the interactive Swagger documentation at:

http://localhost:3000/api-docs

API Endpoints

Get All Notes

  • Endpoint: GET /api/notes
  • Description: Retrieve all notes from the database, ordered by creation date (newest first)
  • Response: Array of note objects
  • Example Response:
    [
      {
        "id": 1,
        "title": "First Note",
        "content": "This is my first note",
        "created_at": "2023-01-01T12:00:00.000Z"
      },
      {
        "id": 2,
        "title": "Second Note",
        "content": "This is my second note",
        "created_at": "2023-01-02T12:00:00.000Z"
      }
    ]

Get Single Note

  • Endpoint: GET /api/notes/:id
  • Description: Retrieve a single note by its ID
  • Parameters:
    • id (path parameter): The ID of the note to retrieve
  • Response: Note object
  • Error Responses:
    • 404 Not Found: If the note with the specified ID doesn't exist
  • Example Response:
    {
      "id": 1,
      "title": "First Note",
      "content": "This is my first note",
      "created_at": "2023-01-01T12:00:00.000Z"
    }

Create Note

  • Endpoint: POST /api/notes
  • Description: Create a new note
  • Request Body:
    {
      "title": "New Note",
      "content": "This is a new note"
    }
  • Required Fields: title, content
  • Response: Created note with ID
  • Error Responses:
    • 400 Bad Request: If title or content is missing
  • Example Response:
    {
      "message": "Note created successfully",
      "id": 3,
      "title": "New Note",
      "content": "This is a new note"
    }

Update Note

  • Endpoint: PUT /api/notes/:id
  • Description: Update an existing note
  • Parameters:
    • id (path parameter): The ID of the note to update
  • Request Body:
    {
      "title": "Updated Note",
      "content": "This is an updated note"
    }
  • Required Fields: title, content
  • Response: Success message
  • Error Responses:
    • 400 Bad Request: If title or content is missing
    • 404 Not Found: If the note with the specified ID doesn't exist
  • Example Response:
    {
      "message": "Note updated successfully"
    }

Delete Note

  • Endpoint: DELETE /api/notes/:id
  • Description: Delete a note by its ID
  • Parameters:
    • id (path parameter): The ID of the note to delete
  • Response: Success message
  • Error Responses:
    • 404 Not Found: If the note with the specified ID doesn't exist
  • Example Response:
    {
      "message": "Note deleted successfully"
    }

Database Schema

The application automatically creates a notes table with the following structure if it doesn't exist:

CREATE TABLE IF NOT EXISTS notes (
  id INT AUTO_INCREMENT PRIMARY KEY,
  title VARCHAR(255) NOT NULL,
  content TEXT NOT NULL,
  created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
)

Project Structure

Online_sessions/
├── main.js          # Main application file
├── public/          # Static files directory
├── package.json     # Project dependencies and scripts
├── node_modules/    # Installed dependencies
└── README.md        # This file

Environment Variables

The application can be configured using the following environment variable:

  • PORT: The port on which the server will listen (default: 3000)

Example:

PORT=8080 node main.js

Contributing

  1. Fork the repository
  2. Create a feature branch (git checkout -b feature/amazing-feature)
  3. Commit your changes (git commit -m 'Add some amazing feature')
  4. Push to the branch (git push origin feature/amazing-feature)
  5. Open a Pull Request

License

This project is licensed under the MIT License - see the LICENSE file for details.

Support

For support, please open an issue in the repository or contact the development team.

About

A modern, RESTful API for managing notes with a clean and intuitive interface. This application provides a robust backend service for creating, reading, updating, and deleting notes with persistent storage using MySQL.

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors