This is a simple REST API built with Node.js, Express.js, and MySQL. It provides endpoints for managing words in a MySQL database.
- CRUD operations for managing words.
- Environment variable support using
dotenv
. - MySQL database integration.
- Automatic database table creation on service startup.
Ensure you have the following installed:
git clone <repository_url>
cd node-mysql-api
npm install
Create a .env
file in the root directory and add the following variables:
DB_HOST=localhost
DB_USER=root
DB_PASSWORD=password
DB_NAME=word_database
PORT=3000
Ensure your MySQL server is running and replace the values as needed.
node index.js
The server should now be running on http://localhost:3000
.
GET /api/words
GET /api/words/:id
POST /api/words
- Body:
{ "word": "example" }
PUT /api/words/:id
- Body:
{ "word": "updated_value" }
DELETE /api/words/:id
GET /health
The service automatically creates the words
table if it does not exist when the server starts.
If you prefer to manually set up the database:
- Log in to MySQL:
mysql -u root -p
- Create the database:
CREATE DATABASE word_database;
- Select the database:
USE word_database;
- Create the table:
CREATE TABLE words ( id INT AUTO_INCREMENT PRIMARY KEY, word VARCHAR(255) NOT NULL );
docker run --name mysql-container \
-e MYSQL_ROOT_PASSWORD=password \
-e MYSQL_DATABASE=word_database \
-e MYSQL_USER=myuser \
-e MYSQL_PASSWORD=mypassword \
-p 3306:3306 -d mysql:latest
docker build -t node-javascript-mysql-demo .
docker run -p 3000:3000 node-javascript-mysql-demo -e DB_HOST=localhost -e DB_USER=root -e MYSQL_ROOT_PASSWORD=password