In this article, we will develop APIs in Node JS for back-end operations and implement a CRUD operation in it.
Prerequisites
Create a new blank folder on your desired location. Now, open cmd and go to the newly created location and write " code . " to open folder in visual studio code.
- In the cmd write below code to initiate the node app by default configurations.
npm init //press enter until you see package.json in the folder structure
At this point of time, a blank package.json will be added into the project.
- We have created basic node app now let's import our required dependencies and npm packages. We will be using express, mysql2, and,nodemon npm packages
npm i express mysql2 nodemon

Database: I have used mysql workbench (you can use any database of your choice)
I have created a simple database called employeedb through MySQL workbench and a table called employee.
Columns in the table and stored procedure for insert and update employee
- EmpId - Int
- Name - Varchar(100)
- EmpCode - Varchar(50)
- Salary - Decimal
CREATE TABLE `employee` (
`EmpId` int NOT NULL AUTO_INCREMENT,
`Name` varchar(100) NOT NULL,
`EmpCode` varchar(40) NOT NULL,
`Salary` int DEFAULT NULL,
PRIMARY KEY (`EmpId`)
) ENGINE=InnoDB AUTO_INCREMENT=49 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci;
Stored procedure for insert and update employee - EmployeeAddOrEdit
CREATE DEFINER=`root`@`localhost` PROCEDURE `EmployeeAddOrEdit`(
IN _EmpId INT,
IN _Name varchar(100),
IN _EmpCode varchar(40),
IN _Salary INT)
BEGIN
IF _EmpId= 0 THEN
INSERT INTO employee (Name, EmpCode,Salary)
Values (_Name,_EmpCode,_Salary);
SET _EmpId = LAST_INSERT_ID();
ELSE
Update employee
SET
Name = _Name,
EmpCode=_EmpCode,
Salary = _Salary
Where EmpId=_EmpId;
END IF;
Let's create config.js file to store database credentials. Create new file called "config.js" and write below code.
const config = {
app: {
port: 3000
},
db: {
host: 'localhost',
user: 'root',
password: 'softweb#567',
database: 'employeedb',
multipleStatements: true
}
};
Let's import config.js file in the index.js file, create a connection string and connect to the database as well as we will listen to the 3000 port through express server.
const mysql = require('mysql2');
const express = require('express');
var app = express();
app.use(express.json());
const config = require('./config');
const { db: { host, user, password, database, multipleStatements }, app: { port } } = config;
var connection = mysql.createConnection({
host: host,
user: user,
password: password,
database: database,
multipleStatements: multipleStatements
});
connection.connect((err) => {
if (!err)
console.log('Database connected successfully');
else
console.log('Database connection failed' + JSON.stringify(err, undefined, 2));
});
app.listen(port, () => console.log('Express server started at port no : ' + port));
To check whether our code is working or not run the node application and see the logs.
node index.js






Join the conversation! Your thoughts help the community grow.