Create a REST API with Foal

Image from

In this article, I want to share how to build REST API with Foal. So, What is Foal? From the official website

Foal (or FoalTS) is a Node.JS framework for creating web applications.It provides a set of ready-to-use components so you don't have to reinvent the wheel every time. In one single place, you have a complete environment to build web applications. This includes a CLI, testing tools, frontend utilities, scripts, advanced authentication, ORM, deployment environments, GraphQL and Swagger API, AWS utilities, and more. You no longer need to get lost on npm searching for packages and making them work together. All is provided.But while offering all these features, the framework remains simple. Complexity and unnecessary abstractions are put aside to provide the most intuitive and expressive syntax. We believe that concise and elegant code is the best way to develop an application and maintain it in the future. It also allows you to spend more time coding rather than trying to understand how the framework works.Finally, the framework is entirely written in TypeScript. The language brings you optional static type-checking along with the latest ECMAScript features. This allows you to detect most silly errors during compilation and improve the quality of your code. It also offers you autocompletion and a well documented API.

First, make sure you installed Node.JS and then install Foal with command

npm install -g @foal/cli

And then running the command

foal createapp my-app

You can change my-app to your app name. After running the command in the above, the default project like structure like in below :

Run this project with command

npm run develop

Open URL localhost:3001 and will show an image like in below

Calm, the journey will begin from now :), so prepare your IDE. FoalTS uses TypeORM as default Object-Relational Mapping. Don’t forget to install a driver.

Install mysql or mysql3 drivers.

npm install mysql --save # mysql2 is also supported

Install pg driver.

npm install pg --save

In this tutorial I using MySQL for the database. Change file default.json in folder config.

Next, create or modify a file user.entity.ts in folder src > app > entities. Change the structure id and email according to your needs.

I was amazed at this command, it will generate a migration file from the entity

npm run makemigrations

Run the command, it will generate from migration to table in the database

npm run migrations

Next, we will create magic with command

foal generate rest-api user --register

It will create and update for files :

CREATE src/app/entities/user.entity.ts
UPDATE src/app/entities/index.ts
CREATE src/app/controllers/user.controller.ts
CREATE src/app/controllers/user.controller.spec.ts
UPDATE src/app/controllers/index.ts
UPDATE src/app/app.controller.ts

Be careful, please check your file. Next, we will test the application use postman. Run command :

npm run develop

GET All Users localhost:3001/users

GET User By ID localhost:3001/users/:id

Post User localhost:3001/users

Body :


Update User localhost/users/:id

Body :


Delete User localhost:3001/users/:id

Software Development Company —

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store