This page looks best with JavaScript enabled

Create a simple to-do API with FeathersJS

 ·   ·  ☕ 4 min read

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.

1
npm install -g @feathersjs/cli

Create project.

1
2
3
md todof
cd todof
feathers generate app todof

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.

1
feathers g service todo

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 by todo.)
  • create todo.model.js in models folder
  • register newly created modules

Start your server.

1
npm run dev

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.

1
2
3
4
5
6
7
{
  "name": "NotAuthenticated",
  "message": "Not authenticated",
  "code": 401,
  "className": "not-authenticated",
  "errors": {}
}
Create user and authenticate

Let’s create a user and use that user to authenticate.

  1. Create a POST request to http://localhost:3030/users to create new user
1
2
3
4
{
  "email": "a@test.com",
  "password": "abcde"
}
  1. 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 -

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
{
  "accessToken": "eyJhbGciOiJIUzI1NiIsInR5cCI6ImFjY2VzcyJ9.eyJpYXQiOjE1Njg2OTY3NTMsImV4cCI6MTU2ODc4MzE1MywiYXVkIjoiaHR0cHM6Ly95b3VyZG9tYWluLmNvbSIsImlzcyI6ImZlYXRoZXJzIiwic3ViIjoidFUwTWNwNndFa0xTd1A0ZyIsImp0aSI6IjIxMDViMTVhLTdmMzUtNDA1ZS04NTk2LTQ4MWExNDdiMjNhNCJ9.6JhToJthfiSwtAGXQ7Qwtt4y14ACryvTU0gM9z30AbE",
  "authentication": {
    "strategy": "local"
  },
  "user": {
    "email": "a@test.com",
    "_id": "tU0Mcp6wEkLSwP4g"
  }
}

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 -

1
2
3
4
5
6
{
  "total": 0,
  "limit": 10,
  "skip": 0,
  "data": []
}

Cool..!

Now, create a to-do record, or two. Create a POST request to http://localhost:3030/todo.

1
2
3
4
{
  "title": "Install feathers",
  "completed": "true"
}

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/.

feathers-nedb-folder

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 -

1
2
3
{
  "title": "Rule the universe"
}

Response -

1
2
3
4
5
{
  "title": "Rule the universe",
  "completed": "false",
  "_id": "1pRKub3hU6ft7YNE"
}
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.

1
2
3
4
5
{
  "title": "Rule the universe",
  "completed": "false",
  "_id": "1pRKub3hU6ft7YNE"
}
Delete to-do

Create yet another request - DELETE http://localhost:3030/todo/1pRKub3hU6ft7YNE

Request -

< no body >

Response -

1
2
3
4
5
{
  "title": "Rule the universe",
  "completed": "false",
  "_id": "1pRKub3hU6ft7YNE"
}

Take stock of what you did so far

Looking back - we just used two commands -

1
2
feathers g app todof
feathers g service todo

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 :).

Stay in touch!
Share on

Prashanth Krishnamurthy
WRITTEN BY
Prashanth Krishnamurthy
Technologist | Creator of Things