Skip to content

Commit e7966ba

Browse files
committed
first commit
0 parents  commit e7966ba

File tree

1 file changed

+125
-0
lines changed

1 file changed

+125
-0
lines changed

README.md

+125
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
1+
# Node.js + MySQL REST API
2+
3+
This is a simple REST API built with **Node.js**, **Express.js**, and **MySQL**. It provides endpoints for managing words in a MySQL database.
4+
5+
## Features
6+
7+
- CRUD operations for managing words.
8+
- Environment variable support using `dotenv`.
9+
- MySQL database integration.
10+
- Automatic database table creation on service startup.
11+
12+
## Prerequisites
13+
14+
Ensure you have the following installed:
15+
16+
- [Node.js](https://nodejs.org/) (v14+ recommended)
17+
- [MySQL](https://www.mysql.com/) database
18+
19+
## Project Setup
20+
21+
### 1. Clone the Repository
22+
23+
```sh
24+
git clone <repository_url>
25+
cd node-mysql-api
26+
```
27+
28+
### 2. Install Dependencies
29+
30+
```sh
31+
npm install
32+
```
33+
34+
### 3. Configure Environment Variables
35+
36+
Create a `.env` file in the root directory and add the following variables:
37+
38+
```env
39+
DB_HOST=localhost
40+
DB_USER=root
41+
DB_PASSWORD=password
42+
DB_NAME=word_database
43+
PORT=3000
44+
```
45+
46+
Ensure your MySQL server is running and replace the values as needed.
47+
48+
### 4. Start the Server
49+
50+
```sh
51+
node index.js
52+
```
53+
54+
The server should now be running on `http://localhost:3000`.
55+
56+
## API Endpoints
57+
58+
### 1. Get All Words
59+
60+
**GET** `/api/words`
61+
62+
### 2. Get a Word by ID
63+
64+
**GET** `/api/words/:id`
65+
66+
### 3. Create a Word
67+
68+
**POST** `/api/words`
69+
70+
- Body: `{ "word": "example" }`
71+
72+
### 4. Update a Word
73+
74+
**PUT** `/api/words/:id`
75+
76+
- Body: `{ "word": "updated_value" }`
77+
78+
### 5. Delete a Word
79+
80+
**DELETE** `/api/words/:id`
81+
82+
### 6. Health Check
83+
84+
**GET** `/health`
85+
86+
## Database Setup
87+
88+
### Automatic Table Creation
89+
The service automatically creates the `words` table if it does not exist when the server starts.
90+
91+
### Manual Database Setup (Optional)
92+
If you prefer to manually set up the database:
93+
94+
1. Log in to MySQL:
95+
```sh
96+
mysql -u root -p
97+
```
98+
2. Create the database:
99+
```sql
100+
CREATE DATABASE word_database;
101+
```
102+
3. Select the database:
103+
```sql
104+
USE word_database;
105+
```
106+
4. Create the table:
107+
```sql
108+
CREATE TABLE words (
109+
id INT AUTO_INCREMENT PRIMARY KEY,
110+
word VARCHAR(255) NOT NULL
111+
);
112+
```
113+
114+
## Local Development
115+
116+
### Run Mysql
117+
118+
```
119+
docker run --name mysql-container \
120+
-e MYSQL_ROOT_PASSWORD=password \
121+
-e MYSQL_DATABASE=word_database \
122+
-e MYSQL_USER=myuser \
123+
-e MYSQL_PASSWORD=mypassword \
124+
-p 3306:3306 -d mysql:latest
125+
```

0 commit comments

Comments
 (0)