This section, we will create basic CRUD RESTful API using Express.js with TypeORM to connect PostgreSQL and try it on Postman.

1.Install RDBMS

In this section, we will use PostgreSQL. The world's most advanced open source database. Please make sure to install PostgreSQL first.

2.Install Express.js

If we not have Node,js command, we need to install Node.js first. Create our Development Directory and open Node.js command into the Development Directory. Then run the command.

npm install express

The command will generate the folder/files.

node_modules/
package.json
package-lock.json

3.Install TypeORM

To install TypeORM with the required libraries, we need to run 3 commands.

npm install typeorm
npm install reflect-metadata
npm install @types/node

4.Install Database Driver

In this section, we will install the PostgreSQL driver. Run the command.

npm install pg

5.Create new Project

To create a new Project with starter set of files and extend the above installed libraries, run this command.

npx typeorm init --name MyProject --database postgres

We can change “MyProject” up to our “Project Name”. This command will generate a new project in the [Project Name] directory with the following files:

[Project Name]

├── src
│   ├── entity
│   │   └── User.ts
│   ├── migration
│   ├── data-source.ts
│   └── index.ts
├── .gitignore
├── package.json
├── README.md
└── tsconfig.json

Then go to the new project directory and run library updates using below commands.

cd [Project Name]
npm install

After we have all dependencies installed, update the "data-source.ts" file for setting our Database Connection Information.

Finally, Test our environments using the below command.

npm start

The starter “index.ts” will create a Database Table and insert a sample record for testing, Then we can go to the next step.

6.Create Controllers

Create directory “controllers” inside “src” and create directory “users” inside “controllers”. Then create database controller files.
Browse my controller files in github.

7.Create Controllers Routing

Create directory “routes” inside “src” and Create “users.ts” file, Then setting our routes inside “users.ts” file.

8.Update Main Script

Before updating the main script, we need to install more useful libraries.

We need a Cross-Origin setting to activate the website that can make API Requests.

npm install cors

And need the Parser.

npm install @types/body-parser

Then update the “tsconfig.json” file to enable esModuleInterop.

Now, we can update the main script to set our Controller Routes in “index.ts”.

Then run the command

npm start

Finally, we can test our RESTful API using Postman or fetching into Front-End Project.

If we need to stop our service press Ctrl+C, then put "y" to confirm stop and Enter.

9.Improve Development Tool

Install nodemon

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

npm install nodemon

Then update the “package.json” file to set the command that can run our project script.

From

   "scripts": {
      "start": "ts-node src/index.ts",
      …
   }

To

   "scripts": {
      "start": "nodemon src/index.ts",
      …
   }

Now, we don’t need to restart our app when our project script has been updated.

For visitor who are need my source-code, this is my github project link:

https://github.com/zodic/express-typeorm1/tree/main

See ya next time m(_ _)m