This tutorial will walk you through creating a Restful API managing a Todo data with some security features. The detail requirements of our API will be like below.
- User register using email, name and password .
- Provided login using email and password which returned JWT token.
- Provide CRUD for TODO data, any user can create TODO, but only the owner of TODO can modify and delete it.
- Provide CRUD to add comment to specific TODO, any user can add comments to specific TODO, but only the owner can modify and delete it
- Provide endpoint to upload picture used for user profile picture
List of endpoints will be created for above requirements are like below.
|Functions||Path||Http Method||Accessible By|
|Create a TODO||POST||All login users|
|Modify/delete TODO by ID||PUT PATCH DELETE||Owner of TODO|
|Get TODO by id||GET||All login users|
|Get list of TODOs with filter and pagination||GET||Public|
|Add comment to specific todo||POST||All login users|
|Modify/remove comment to specific todo||PUT PATCH DELETE||Owner of comment|
To be able to follow this example you need some software installed in your computer.
- Node.js v10 or newer
- Any terminal app
- Any text editor (VSCode preferred)
Confirm that Node.js installed properly on your machine by execute command below in your terminal
Above command will download Degit locally then download Plumier blank project starter into
If you having problem executing
npx you can manually install Degit globally
Then followed by executing Degit manually like below
Enter to the project directory by executing command below
Install package dependencies by executing command below
Open the project directory using your favorite text editor or IDE, on this example we will use VSCode.
Plumier blank project starter contains minimal files required to create API with Node.js and TypeScript. The project structure is like below
There are more project starter available on the
plumier/starter repository on each appropriate branch.
Next step we will install some NPM package required to build our API. The project starter already provided some basic packages required.
Next we will install TypeORM package for data access, we will use SQLite 3 database to store the user data. TypeORM supported several databases so it will be possible to change your configuration later appropriately.
Back to your terminal and execute command below
Above code will install TypeORM package, Plumier TypeORM helper and SQLite driver into the package configuration file.
Now we can start modelling our User by creating TypeORM entity. We define
User entity with properties below
Next we will add TypeORM configuration to be able to connect to our database. We will connect to SQLite in memory database. Open
.env file and copy paste source code below
Next we will also add Swagger to our project for our front end developer to be able to inspect the request and response of our API.
Our project now is ready to start, go back to terminal app and execute command below
If you are follow the steps above correctly, the terminal will print output like below
We will also add login functionalities to our project using JWT token, and hash password using bcrypt. Install the appropriate packages like below.