Building Node.js Typescript Express and MySQL: CRUD Rest API example.
| Methods | Urls | Actions |
|---|---|---|
| GET | api/tutorials | get all Tutorials |
| GET | api/tutorials/:id | get Tutorial by id |
| POST | api/tutorials | add new Tutorial |
| PUT | api/tutorials/:id | update Tutorial by id |
| DELETE | api/tutorials/:id | remove Tutorial by id |
npm install
npm run start
Connect to MySQL instance and create database and table as per below:
create database testdb; use testdb;
CREATE TABLE IF NOT EXISTS tutorials (
id int NOT NULL PRIMARY KEY AUTO_INCREMENT,
title varchar(255) NOT NULL,
description varchar(255),
published boolean DEFAULT false
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
INSERT INTO tutorials (title, description, published) VALUES ('Introduction to SQL', 'Learn the basics of SQL, including queries and joins.', true), ('Advanced SQL Techniques', 'Explore subqueries, indexes, and optimization.', false), ('Node.js for Beginners', 'Start building backend applications using Node.js.', true), ('REST APIs with Express', 'Learn how to create RESTful APIs using Express.js.', false), ('Frontend Basics', 'HTML, CSS, and JavaScript essentials.', true), ('Angular Crash Course', 'Build dynamic web applications using Angular framework.', false), ('React Fundamentals', 'Understand React components, state, and props.', true), ('Database Design', 'Learn how to design relational databases efficiently.', false), ('Docker Essentials', 'Containerize your applications with Docker.', true), ('DevOps Introduction', 'Basics of CI/CD, Jenkins, and cloud deployments.', false);
Once Table is created update application source code and then deploy again by editing below file:
src/config/db.config.ts