After reading this tutorial you will know how to create Node.js API with Express.js and MySQL database using MDB GO.
Many people think that when you use Node.js you have to use MongoDB for storing data. But that's not true. You can use any kind of storage - it simply depends on your use case. In this tutorial we will show you how to create a Node.js API with MySQL database.
Here's a quick recap what are the main technologies used in this tutorial:
We won't dive deep into details about how to get started with these technologies using MDB GO. Go ahead and check the linked tutorials and once you learn a few things get back here.
If you have enough knowledge to get started, let's do this.
Our example app will be a TODO list. It's a very popular use-case so let's simply get things done one at a time. We will go through the following steps:
todo_app
Let's go!
In order to create a new database you need to run the following command:
$ mdb database init -db mysql8
Now you need to provide your user data and then the database details. Please provide the values of your choice. For example:
? Enter username thor
? Enter password Mjolnir_1
? Repeat password Mjolnir_1
? Enter database name todo_app
? Enter description Database for the TODO app
Note: the password must contain at least one uppercase letter, one lowercase letter, one number, one special symbol and have minimum length of 8.
Hit Enter and it's done.
Please notice that in the command output the username and database name slightly differs from what you provided earlier. Don't worry - it's OK. A few random characters have been added to these values to randomize them and improve security of your database credentials.
Important: Do not close your terminal window until you save your credentials somewhere. This is the only time we will show you your database password. If you don't save it you'll lose it.
That's pretty much it. You can now see how to connect with this database from the Node.js API.
In case you don't know, Sequelize is an object-relational mapper (ORM) built for Node.js. It helps managing MySQL access and translates the database results into easy-to-use objects. You can initialize a MDB GO starter that already has configured Sequelize. Simply run the following command:
$ mdb backend init
and choose Express.js + MySQL API starter
from the list that shows up.
After initialization just go to the newly created project directory and open the
.env
file. After that edit the DB_CONNECTION_STRING
value. You
should paste the connection string that's been shown to you in the previous step. In my
case the updated value looks like this:
DB_CONNECTION_STRING=mysql://thor83ad51bf:Mjolnir_1@mysql.db.mdbgo.com:3306/todo_app48a0ca47
Save the file and close it.
The starter project is ready to use. You don't have to change anything to run the example app for the sake of this tutorial. Simply publish it using the following command:
$ mdb publish -p node12
</code>
After your files get uploaded you can access your app under the address shown in the output.
Note: You may want to check the tutorial where we explain in detail what can go wrong here and how to deal with it.
Note: Since we need to install dependencies and run your app, it may take a few moments until it will be available under the provided URL.
Alright, let's see how to "talk" with our fresh API.
Before we jump into "talking" stuff, firstly let's find out what endpoints do we have.
In the ./app/routes/tasks.js
file there are a few endpoints defined to
show you how to get started.
By default on the /
route there is the index.html
file
served (located in ./public/
folder). There are also four additional
endpoints defined:
GET /tasks
- reads the tasks
array and sends it as a
response
POST /tasks
- creates a new task and appends it to the
tasks
array
PUT /tasks/:taskId
- updates a specific taskDELETE /tasks/:taskId
- deletes a specific task
And these are the ones we are going to talk to. In order to do that I recommend you
install some REST client - like Postman for example - because it'll make your life
way easier. Alternatively, you can utilize curl
command available in
Linux/MacOS terminals. However, in this tutorial we will go with Postman.
Go ahead and install it using official docs.
Once you download and install Postman you can start making requests. We will cover all of the above requests.
In order to make a GET request you need to create a new request in Postman and do two things:
https://express-sequelize-starter.mdbgo.io/tasks
Once you hit Send
you will see the response - an empty array. Let's add
an item there.
In order to create a task we need to make a POST request. In order to do that you need to:
https://express-sequelize-starter.mdbgo.io/tasks
{
"name": "Do laundry",
"desc": "Boring but necessary..."
}
Once you hit Send
you will see the response - a new task with
id
1. Go ahead and run the GET
request again to see it.
Now let's try to update it.
Updating the task requires the following steps:
https://express-sequelize-starter.mdbgo.io/tasks/1
{
"name": "Do ironing",
"desc": "Boring but necessary... and also quite dangerous!"
}
Once you hit Send
you will see the OK
response. Go ahead
and run the GET
request again to see it. Alright, let's delete it.
In order to delete a task you need to do the following:
https://express-sequelize-starter.mdbgo.io/tasks/1
Once you hit Send
you will see the OK
response - it means
the task has been deleted. If you run the GET
request again you will
see that the task is gone.