George Whitcher

CRUD with nodejs, MySQL, and express

CRUD with nodejs, MySQL, and express

A little background

I have been rewriting my bot NOL with a web admin and needed an API so they can interact with the database. Since the bot is run using nodejs I wanted to keep it in node rather than use PHP. I also wanted to use MySQL. Using express it was actually super easy to accomplish this.

This API is as standard as it gets and I built it to work across any table with any column. It has all the necessary CRUD (Create, Read, Update, Delete) functions. That said depending on your needs and what you are using it for you may have to refine it. It is also not very secure and the only security is the unique API key that I add to the end of all the endpoints. While this is not the most secure and is not perfect, this should be more than enough to get anyone started that is interested in making their own API using nodejs.

Create

				
					/insert/TABLE-NAME/API-KEY?column1=value1&column2=value2
				
			
  • Replace “TABLE-NAME” with your table name.
  • Replace “API-KEY” with the key from your config.
  • Replace “column1” with the column of the table you want to target.
  • Replace “value1” with the value you want to set for that column.
  • Repeat for “column2“.

READ

				
					/select/TABLE-NAME/API-KEY
				
			
  • Replace “TABLE-NAME” with your table name.
  • Replace “API-KEY” with the key from your config.

Update

				
					/update/TABLE-NAME/ID/API-KEY?column1=value1&column2=value2
				
			
  • Replace “TABLE-NAME” with your table name.
  • Replace “ID” with the ID of the MySQL row you want to update.
  • Replace “API-KEY” with the key from your config.
  • Replace “column1” with the column of the table you want to target.
  • Replace “value1” with the value you want to set for that column.
  • Repeat for “column2“.

Delete

				
					/delete/TABLE-NAME/ID/API-KEY
				
			
  • Replace “TABLE-NAME” with your table name.
  • Replace “ID” with the ID of the MySQL row you want to delete.
  • Replace “API-KEY” with the key from your config.

The Code

Sharing is caring!
Share on facebook
Share on twitter
Share on linkedin
Share on reddit
Share on pinterest
Share on email
Share on print