{"id":29228289,"url":"https://github.com/basuru07/nodejs-api","last_synced_at":"2025-07-03T10:10:16.759Z","repository":{"id":301406687,"uuid":"1009125547","full_name":"basuru07/nodejs-api","owner":"basuru07","description":null,"archived":false,"fork":false,"pushed_at":"2025-06-26T17:37:46.000Z","size":1492,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":0,"default_branch":"main","last_synced_at":"2025-06-26T18:29:34.131Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/basuru07.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null,"zenodo":null}},"created_at":"2025-06-26T16:05:02.000Z","updated_at":"2025-06-26T17:37:50.000Z","dependencies_parsed_at":"2025-06-26T18:31:17.708Z","dependency_job_id":"cd84a214-9f2f-4050-a8fb-5eacfa81677f","html_url":"https://github.com/basuru07/nodejs-api","commit_stats":null,"previous_names":["basuru07/nodejs-api"],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/basuru07/nodejs-api","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/basuru07%2Fnodejs-api","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/basuru07%2Fnodejs-api/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/basuru07%2Fnodejs-api/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/basuru07%2Fnodejs-api/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/basuru07","download_url":"https://codeload.github.com/basuru07/nodejs-api/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/basuru07%2Fnodejs-api/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":263305276,"owners_count":23445865,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2025-07-03T10:10:15.972Z","updated_at":"2025-07-03T10:10:16.744Z","avatar_url":"https://github.com/basuru07.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Node.js REST API with MySQL\n\nThis project is a RESTful API built with Node.js, Express, and MySQL, providing user authentication and product management functionalities. It includes secure user registration/login with JWT authentication and CRUD operations for managing products.\n\n## Features\n\n- **User Authentication**:\n  - Register new users with username, email, and password\n  - Login with email and password to receive a JWT token\n  - Password hashing using bcryptjs\n  - JWT-based authentication for protected routes\n- **Product Management**:\n  - Create, read, update, and delete (CRUD) products\n  - Input validation for product data\n- **Security**:\n  - Parameterized SQL queries to prevent SQL injection\n  - CORS configuration\n  - Consistent error handling\n- **API Response Format**:\n  - Standardized JSON responses with `success`, `message`, and `data` fields\n\n## Prerequisites\n\n- Node.js (v14 or higher)\n- MySQL Server\n- A code editor (e.g., VS Code)\n- Postman or similar API testing tool\n\n## Installation\n\n1. **Clone the Repository** (if applicable):\n   ```bash\n   git clone https://github.com/basuru07/nodejs-api.git\n   cd nodejs-api\n   ```\n\n2. **Install Dependencies**:\n   ```bash\n   npm install\n   ```\n\n3. **Set Up MySQL Database**:\n   - Create a MySQL database and tables by running the following SQL commands:\n     ```sql\n     CREATE DATABASE nodejs_api;\n     USE nodejs_api;\n\n     CREATE TABLE users (\n         id INT AUTO_INCREMENT PRIMARY KEY,\n         username VARCHAR(50) UNIQUE NOT NULL,\n         email VARCHAR(100) UNIQUE NOT NULL,\n         password VARCHAR(255) NOT NULL,\n         created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP\n     );\n\n     CREATE TABLE products (\n         id INT AUTO_INCREMENT PRIMARY KEY,\n         name VARCHAR(100) NOT NULL,\n         price DECIMAL(10, 2) NOT NULL,\n         quantity INT NOT NULL DEFAULT 0,\n         created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n         updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP\n     );\n     ```\n\n4. **Configure Environment Variables**:\n   - Create a `.env` file in the project root and add the following:\n     ```env\n     PORT=3000\n     DB_HOST=localhost\n     DB_USER=root\n     DB_PASSWORD=your_mysql_password\n     DB_NAME=nodejs_api\n     JWT_SECRET=your_jwt_secret_key_here\n     ```\n\n5. **Run the Application**:\n   - For development (with auto-restart):\n     ```bash\n     npm run dev\n     ```\n   - For production:\n     ```bash\n     npm start\n     ```\n\n## Project Structure\n\n```\nnodejs-api/\n├── config/\n│   └── database.js          # MySQL connection configuration\n├── controllers/\n│   ├── authController.js    # User authentication logic\n│   └── productController.js # Product CRUD logic\n├── middleware/\n│   └── auth.js             # JWT authentication middleware\n├── models/\n│   ├── User.js             # User model for database operations\n│   └── Product.js          # Product model for database operations\n├── routes/\n│   ├── auth.js            # Authentication routes\n│   └── products.js        # Product routes\n├── .env                   # Environment variables\n├── server.js              # Main server file\n├── package.json           # Project metadata and scripts\n└── README.md              # Project documentation\n```\n\n## API Endpoints\n\n### Authentication\n- **Register User**:\n  - `POST /api/auth/register`\n  - Body: `{ \"username\": \"john_doe\", \"email\": \"john@example.com\", \"password\": \"password123\" }`\n- **Login User**:\n  - `POST /api/auth/login`\n  - Body: `{ \"email\": \"john@example.com\", \"password\": \"password123\" }`\n\n### Products\n- **Get All Products**:\n  - `GET /api/products`\n- **Get Product by ID**:\n  - `GET /api/products/:id`\n- **Create Product**:\n  - `POST /api/products`\n  - Body: `{ \"name\": \"iPhone 15\", \"price\": 999.99, \"quantity\": 50 }`\n- **Update Product**:\n  - `PUT /api/products/:id`\n  - Body: `{ \"name\": \"iPhone 15 Pro\", \"price\": 1199.99, \"quantity\": 30 }`\n- **Delete Product**:\n  - `DELETE /api/products/:id`\n\n**Note**: Product routes (POST, PUT, DELETE) require a valid JWT token in the `Authorization` header as `Bearer \u003ctoken\u003e`.\n\n## Example API Response\n\n```json\n{\n    \"success\": true,\n    \"message\": \"Operation successful\",\n    \"data\": {\n        // Response data\n    }\n}\n```\n\n## Error Handling\n\nThe API handles:\n- Validation errors (400)\n- Authentication errors (401)\n- Not found errors (404)\n- Server errors (500)\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fbasuru07%2Fnodejs-api","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fbasuru07%2Fnodejs-api","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fbasuru07%2Fnodejs-api/lists"}