Create a simple to-do API using FeathersJS v4.
FeathersJS v4 launched yesterday, and I wanted to see what changed. Of course, I wasn’t going to write a post just about v4 - I have not kept up with the Feathers ecosystem. So, let’s refresh our mind and soul by building a quick API and see how the story develops.
We can start with Feathers using their CLI or with just a NPM install. We will use feathers CLI for the purpose of this post because of two reasons -
- the CLI is simply awesome
- I am lazy - feathersjs CLI saves a lot of typing
Install and initialize project
Install Feathers CLI globally.
|
|
Create project.
|
|
Select defaults for all questions. After the process completes you should have a new project folder (todof
) with all packages installed and with an updated package.json
.
Explore the project folder in VSCode.
Start coding an API
Create a new service. A service is an instance of a class and contains all operations for a particular entity.
|
|
feathers g
is short for feathers generate
.
The command will -
- create a
todo
folder and a class, hook and service file within the folder (prefixed bytodo.
) - create
todo.model.js
inmodels
folder - register newly created modules
Start your server.
|
|
This will start a feathers server at port 3030 by default.
Run your new API
Go to Insomnia (or your favourite client).
Create a GET request to http://localhost:3030/todo
You will receive an 401 error if you had accepted all defaults while creating the service.
|
|
Create user and authenticate
Let’s create a user and use that user to authenticate.
- Create a POST request to
http://localhost:3030/users
to create new user
|
|
-
Login with the new user. Create a new POST request to
http://localhost:3030/authentication
1 2 3 4 5
{ "email": "a@test.com", "password": "abcde", "strategy": "local" }
FeathersJS supports JSON authentication. Due to defaults configured for the service and the app itself, we see a default response from authentication request like so -
|
|
Copy the access token from the response.
Create/query to-do
We will go back to our to-do GET request and include the token. Submit the request again, and you will see the below response -
|
|
Cool..!
Now, create a to-do record, or two. Create a POST request to http://localhost:3030/todo
.
|
|
These records will be stored in the database we specified while configuring the service. If you accepted defaults, the database will be NeDB (a local datastore) and the data file will be present in <root>/data/
.
Experiment with other services.
Update to-do
Create a new request - PATCH http://localhost:3030/todo/1pRKub3hU6ft7YNE
Note that we used the id received from the earlier GET request in the URL.
Request -
|
|
Response -
|
|
Query single to-do
Create another request - GET http://localhost:3030/todo/1pRKub3hU6ft7YNE
.
This request is very similar to the earlier GET but filters the results by applying the id used in URL.
|
|
Delete to-do
Create yet another request - DELETE http://localhost:3030/todo/1pRKub3hU6ft7YNE
Request -
< no body >
Response -
|
|
Take stock of what you did so far
Looking back - we just used two commands -
|
|
And, generated an awesome app that can support all REST operations for the specified entity!
Isn’t FeathersJS awesome?
But, don’t get teary-eyed yet. Feathers can do a lot more and we will get to those in a while :).