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.
- 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
- 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
- Node.js (v14 or higher)
- MySQL database access
- npm or yarn package manager
-
Clone the repository
git clone https://github.com/bouzayenilyes/wsanotes.git cd wsanotes -
Install dependencies
npm install
-
Database Configuration
The application is configured to connect to a MySQL database. The connection details are already set in the
main.jsfile:- Host: ...
- User: ...
- Password: ...
- Database: ...
- Port: 3307
If you need to use your own database, update the connection details in the
main.jsfile:const db = mysql.createConnection({ host: "your-host", user: "your-username", password: "your-password", database: "your-database", port: 3306 // or your MySQL port });
-
Run the application
node main.js
The server will start on port 3000 (or the port specified in the PORT environment variable).
Once the server is running, you can access the interactive Swagger documentation at:
http://localhost:3000/api-docs
- 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" } ]
- 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" }
- 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" }
- 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 missing404 Not Found: If the note with the specified ID doesn't exist
- Example Response:
{ "message": "Note updated successfully" }
- 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" }
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
)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
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- Fork the repository
- Create a feature branch (
git checkout -b feature/amazing-feature) - Commit your changes (
git commit -m 'Add some amazing feature') - Push to the branch (
git push origin feature/amazing-feature) - Open a Pull Request
This project is licensed under the MIT License - see the LICENSE file for details.
For support, please open an issue in the repository or contact the development team.