Create api with nodejs, express and MySQL

Tram Ho

Before starting, here are the prerequisites and requirements for us to be able to deploy the application:

Node.js is an open source, cross-platform runtime environment for developing network and server-side applications. Everyone should have a basic knowledge of nodejs .

ExpressJS is one of the most popular framework for node.js It is built on top of the node.js http module and adds support for routing, middleware, viewing system, etc.It is very simple and minimal, unlike other frameworks.

MySQL is an open source relational database management system. Its name is a combination of “My”, the name of the daughter of co-founder Michael Widenius and “SQL”, short for Structured Query Language .

EcmaScript (ES) is a standardized scripting language for JavaScript (JS) . The current ES version supported in modern browsers is ES5 . However, ES6 solves many of the core language’s limitations, making it easier for developers to code.

Postman is an API development tool (application programming interface) that helps to build, test and modify API , it is capable of making different types of HTTP requests ( GET , POST , PUT , etc.) .

IDE (Integrated Development Environment) is a software application that provides comprehensive facilities for computer programmers to develop software. An IDE typically includes at least one source code editor, build automation tools, and a debugger. In my case I prefer to use visual studio code.

1. Create project

Create a project with the command below:

2. Project initialization and configuration

To initialize the project we type the command: npm init , if we want to avoid some questions then we type the command: npm init -y . The result is the package.json file below:

3. Install express and other dependencies

Expressjs is a framework built on top of Nodejs .

Body Parser returns a function that acts as a middleware . The function listens on req.on ('data') and builds r eq.body from the chunks of data it receives.

MySQL is an open source database used to interact with databases and manipulate records.

Nodemon is a tool that helps develop node.js based applications by automatically restarting the application when file changes in the directory are detected.

Now check the package.json file again:

4. Initialize and start the server

Create a server.js file in the project root directory and add a few lines of code:

Now let’s run web server up and see:

Open a browser and go to http: // localhost: 5000 . If the browser displays "Welcome to my web server" , then the server you created is up and running.

In the process steps, I installed nodemon . If you want the server to automatically restart when there is a change, we just need to add the following code to the package.json file:

Now we will run the server with the command: npm start

5. Create database

Everyone download MySQL and create their own database. Here I will copy the script I created for everyone to refer.

6. Database connection

To make a connection to the database in the project, create a configuration directory at the root directory and create the db.config.js file inside the config directory.

Next, add the code below to create mysql connection:

If after the connection displays as below, everyone has successfully connected to their database.

7. Project Directory Structure

Let’s take a look at the project directory structure:

Below I will demo writing an api taking all employees according to the MVC model, everyone will see it:

Finally, we update code file server.js :

Okay, the results after I tested the postman everyone.

Reference: https://medium.com/@rahulguptalive/create-crud-apis-in-nodejs-express-and-mysql-abda4dfc2d6

Thank you everyone for watching, see you in the next posts

Share the news now

Source : Viblo