SuperApiBoost is a client that seeks to improve development times.
In this post, I will show you how to make a complete API under NodeJs with Typescript, Mongo, and Express with just two commands.
Making a Complete REST API
The first thing you need to do is install the client with npm.
To generate the project, you must execute the “new” command optionally with the -n
flag to assign the name. (If you don’t set the name of the project, the client will ask you.)
This will create a root folder with the name of the project and install the dependencies.
Awesome
┣ node_modules
┣ src
┃ ┣ api
┃ ┃ ┣ routes
┃ ┃ ┗ index.ts
┃ ┣ controllers
┃ ┃ ┗ mongoBasic.ts #CRUD Methods
┃ ┣ interfaces
┃ ┣ tools #utils functions
┃ ┃ ┗ validateType.ts
┃ ┣ app.ts
┃ ┣ config.ts
┃ ┗ mongo.ts
As an example, we will create a crud for the following JSON.
{
"name":"Andy",
"cellphone":303030303,
"isValid":false
}
First, you need to execute the generate command.
They have two flags. Both are optional.
The first is the name -n=[NAME]
, which is necessary to generate the route and controller.
The second is the -p=[PATH]
, which is the absolute path to locate the .json file of the model. In this case, we will generate the schema with the client.
// history of the execution
$ sabo generate
$ Write a name for the controller: API
$ You want to create a controller without schema? (Use arrow keys)
$ Yes
$ >No
$ Name of the field: name
$ ? Choose a type (Use arrow keys)
$ >string
$ number
$ boolean
$ array
$ ? Is required? (Use arrow keys)
$ >Yes
$ No
$ ? Add a new field? (Use arrow keys) //Repeat the process with every field of the schema
$ >Yes
$ No
This will create the interface, controller, and routes. If you don’t want to generate the schema, the client will generate only the controller and route with an example method and route.
Go to the config.ts
file and assign the connection string of your mongo database.
Execute npm start
, and we will be able to consume the API with our preferred client.