pexels-photo-577585-577585.jpg

Last updated on August 8th, 2024 at 05:41 am

Build a RESTful API with Node.js ,Express and MySQL using mysql2 driver for Node.JS

In this tutorial we are going to explore the process of building a REST API with Node.JS, Express and MySQL, using Node.js driver for MySQL called mysql2 .

By the End of this tutorial you will be able to create api endpoints that can be integrated into any frontend application and communicate with the MySQL database with ease. You will have created separate endpoints to create, retrieve , update and delete data. In our example we will build api for note taking application.

Prerequisites:

  • Node.js & npm installed.
  • mySQL database (local)
  • Code editor /IDE ( we are using VS Code ).
  • Postman – A popular API testing Client or Thunder Client- A VS Code extension for API testing
  • *mySQL Workbench(GUI for mySQL) or SQLTools- a VSCode extension that can work as GUI for SQL based databases- *optional
  • An ounce of Curiosity & Pint of Patience.

Warm-up: Getting familiar with techStack & keywords:

  • Node.JS: A JavaScript runtime built on Chrome’s V8 JavaScript engine. Node.js uses an event-driven, non-blocking I/O model that makes it lightweight and efficient. Its is just the same old JavaScript but now you can run it on your machine outside the browser.In our case we are using it to build a server that will handle our API requests.
  • Express: A minimal and flexible Node.js web application framework that provides a robust set of features for web and mobile applications. We will use Express to build our REST API.
  • mySQL: a widely used relational database management system (RDBMS). It is also free and open-source.
  • REST API: REST stands for Representational State Transfer. It is an architectural style for designing networked applications. A RESTful API is an application program interface (API) that uses HTTP requests to GET, PUT(PATCH), POST and DELETE data. We will build a REST API to manage our notes data.
  • CRUD : CRUD stands for Create, Read, Update, and Delete. These are the basic operations that can be performed on data. We will implement these operations in our API.
  • Postman: A popular API testing tool that allows you to test your APIs by sending HTTP requests and inspecting the responses. We will use Postman desktop application for mac to test our API.
  • Thunder Client: A lightweight Rest API Client Extension for VS Code. You can test your API’s within your IDE so ease your workflow.

Brief overview of the project:

We will build an API with following routes.

MethodEndpointsDescription
POST/notesCreates a new Note
GET/notesFetches all Notes
GET/notes/:idFetches single note by Id
PUT/notes/:idUpdates existing note(by id)
DELETE/notes/:idDeletes single note
DELETE/notesDeletes all notes in the table
Table1: API Routes

Setting up the project:

  1. Create a new directory for the project and navigate to the project directory.
  2. Run `npm init` to initialize the node project. This will create a package.json file.
  3. Install the required dependencies using the following commands:- `npm install express mysql2 dotenv nodemon`
  4. Create the entry way file called `server.js` in the root directory of the project.
  5. Create a new file named `.env` in the root directory of the project and add the following configuration:
    • `HOST=localhost
    • USERDB=root
    • PASSWORD=your_password
    • DB=your_database_name`
  6. Create the following directories in the project root directory:
    • `controllers`
    • `models`
    • `routes`
    • config`
  7. Create the following files in the respective directories:
    • `controllers/noteController.js`
    • `models/note.js`
    • `routes/notes.js`
    • `config/dbConfig.js`

Server setup:

Database Connection:

Model:

Controller:

Routes:

API testing:

Writing in Progress !!

Source Code:

If you would like to checkout the full source code in the mean time, checkout the github repo:

Github:https://github.com/koirpraw/rest-api-nodejs-mysql-notes-app

Praweg

A Curious and Creative fella with knack for coding.

Leave a Reply

Your email address will not be published. Required fields are marked *