Feathers.js— Building REST application (A framework on Node.js)

Amir Mustafa
Geek Culture
Published in
8 min readOct 3, 2021

→ Feathers.js is a real-time, micro-service web framework for NodeJS that gives you control over your data via RESTful resources, sockets and flexible plug-ins.

→ The application can be created in either JavaScript or TypeScript

→ Feathers can interact with any backend technology, supports over a dozen databases and works with any frontend technology like React, VueJS, Angular, React Native, Android or iOS.

Important Features of FeathersJS:

  1. FeathersJS is developed as a micro-service framework for NodeJS and can also be a web application
  2. Supports both Relational and Non-Relational Databases
  3. Simplifies Real-Time Applications Development
  4. Simplifies Development of Service-Oriented Apps
  5. Provides REST APIs Automatically
  6. Supports Authentication
  7. Accelerates Error Handling

What we will understand:

By the end of this article, you will have a good understanding of:

a. Creating authentication using JWT

b. CRUD operations using Feathers.js framework of Node

c. Insomnia

Installation:

npm install @feathersjs/feathers --savenpm install -g @feathersjs/cli

→ Once you have feathers installed open your terminal or command prompt and enter below command

feathers --help

→ We can see a bunch of commands feathers provides — app, authentication, connection, hook, middleware, service, plugin

What we will develop:

Backend: Feathers is a Node.js framework and will handle most backend JS development

Frontend: We will use Insomnia/Postman. We can use any frontend JS framework like React.js, Angular.js or Vue.js

Database: NeDB, The speciality of this database it stores data in memory. We can also use any relational DB like Postgres, MongoDB and no-SQL like MongoDB.

Create New Application:

feathers g app

→ In the terminal it will ask some questions — create via JavaScript or TypeScript: Let us choose JavaScript for this application

→ We will answer the series of questions based on the project requirement. Chose the same as below

→ There are multiple authentication types feature available — Username + Password, Auth0, Google, Facebook, Twitter and GitHub.

Use the space button to select more than one authentication method.

We can choose as per client requirements. Keeping it simple we will choose Username + Password

→ Choose DB as NeDB. Similarly based on the requirement we chose any database

→ Finally, it will install the boilerplate as per our configurations

Directory Structure:

→ For starting the application using any one of the below commands:

yarn dev
npm run dev

→ An application opens in http://localhost:3030

Insomnia:

→ We will use Insomnia for testing our REST APIs. Download here.

→ This is similar to Postman. I wanted to know more about this API client as well. Hence used this one 😄

→ We will set the base URL in the environment variable for this collection.

→ Open Cmd + E (Mac) or Ctrl + E (Windows) to set base variables. This is the key-value setting

baseUrl: 'http://localhost:3030'

→ Let us create our first route:

→ On writing baseUrl will load value from the environment variable

→ We got some responses. Yes, it is 404 for now 😄.

→ Our Insomnia is working. Let us create some meaningful routes.

→ We will be happy to know Feathers have auto-created REST functionality. We will just use the right routes and get or post our data.

Authentication:

→ Feathers use JWT token concept for creating sessions.

→ We are using Username + local authentication (its type is local). As per requirement, there can be more authentication types.

→ We will call the user endpoint.

NOTE: Calling any endpoint will call user hooks first

  1. Register (Create operation):

TYPE: POST

URL: http://3030/users

Request JSON body:

{
"name":"Amir Mustafa",
"email": "amir.mustafa@gmail.com",
"password": "amir@123"
}

Response:

{
"name": "Amir Mustafa",
"email": "amir.mustafa@gmail.com",
"_id": "vUD8zJdDQQsCBLsQ"
}

Entry 2:

→ The data is saved in NeDB in memory (i.e. in a data/* directory). For register in data/users.db file

2. Login:

We need to pass the below details in the Insomnia API Client.

TYPE: POST

URL: http://localhost:3030/authentication

Request JSON body:

{  "email": "raj.khanna@gmail.com",  "password": "raj@123",  "strategy": "local"}

Response:

{
"accessToken": "eyJhbGciOiJIUzI1NiIsInR5cCI6ImFjY2VzcyJ9.eyJpYXQiOjE2MzMxMTEwNjcsImV4cCI6MTYzMzE5NzQ2NywiYXVkIjoiaHR0cHM6Ly95b3VyZG9tYWluLmNvbSIsImlzcyI6ImZlYXRoZXJzIiwic3ViIjoiQTJjWG1SUXV1THRYcnh0aSIsImp0aSI6IjliNGIxMzhmLWMxODEtNDY4Ni04MzViLTQyNzU5Y2JiMDhkZiJ9.k_Uazv4-NLz5WiuziOlt9D8yeUeCLG5fhc2RzmTVN1Q",
"authentication": {
"strategy": "local",
"accessToken": "eyJhbGciOiJIUzI1NiIsInR5cCI6ImFjY2VzcyJ9.eyJpYXQiOjE2MzMxMTEwNjcsImV4cCI6MTYzMzE5NzQ2NywiYXVkIjoiaHR0cHM6Ly95b3VyZG9tYWluLmNvbSIsImlzcyI6ImZlYXRoZXJzIiwic3ViIjoiQTJjWG1SUXV1THRYcnh0aSIsImp0aSI6IjliNGIxMzhmLWMxODEtNDY4Ni04MzViLTQyNzU5Y2JiMDhkZiJ9.k_Uazv4-NLz5WiuziOlt9D8yeUeCLG5fhc2RzmTVN1Q",
"payload": {
"iat": 1633111067,
"exp": 1633197467,
"aud": "https://yourdomain.com",
"iss": "feathers",
"sub": "A2cXmRQuuLtXrxti",
"jti": "9b4b138f-c181-4686-835b-42759cbb08df"
}
},
"user": {
"name": "Amir Mustafa",
"email": "amir.mustafa@gmail.com",
"_id": "A2cXmRQuuLtXrxti"
}
}

→ We successfully got the login response 🙂

CRUD Operation:

→ CRUD stands for Create, Read, Update and Delete operation.

→ In all the routes, the accessToken received from a login is a must that is auto handled from feathers

→ Let us add this in environment variables:

→ We will set tokens in the environment variable for this collection.

Open Cmd + E (Mac) or Ctrl + E (Windows) to set base variables. This is the key-value setting

→ Now for CRUD, let us create a new service vehicle in the same application.

→ To create a new service we use the below command:

feathers g service

→ We will follow the same step that we did at the time of application creation with the only difference of name (vehicles) and path (/vehicles) (refer to above screenshot).

→ Let us check the directory structure in the app:

→ REST code is again auto handled. Let us go to insomnia and executes our routes

  1. Create Vehicles:

Let us create a new directory for Vehicles service where we will keep all the vehicles routes

TYPE: POST

URL: http://localhost:3000/vehicles

Request:

{
"brand": "TATA",
"model": "Grand i10",
"description": "Another success TATA product",
"price": 1500000
}

Chose Auth Type: Bearer token

Response:

{
"brand": "TATA",
"model": "Grand i10",
"description": "Another success TATA product",
"price": 1500000,
"_id": "W05so7ulQWO3RCmG"
}

and the data is inserted

Entry 2:

Entry 3:

→ All the data is store in NeDB memory. Let us check in code

2. Read Vehicles:

→ We can read all the inserted data in vehicles.

→ This can either be getting full data or filtered data based on parameter

A. Read All Data

TYPE: GET

URL: http://localhost:3030/vehicles

Request: No Request call for GET Request

Response:

{
"total": 4,
"limit": 10,
"skip": 0,
"data": [
{
"brand": "Tesla",
"model": "X",
"description": "Awesome electric car",
"price": 1000000,
"_id": "EJP1d7vnKm5RmcFZ"
},
{
"brand": "TATA",
"model": "Grand i10",
"description": "Another success TATA product",
"price": 1500000,
"_id": "IiVncDRAXcNAKCD9"
},
{
"brand": "TATA",
"model": "i20",
"description": "Another success TATA product",
"price": 1500000,
"_id": "UT3Fn2rXqsVQ3DEN"
},
{
"brand": "Tesla",
"model": "Mahindra",
"description": "Great Electric Car",
"price": 2500000,
"_id": "ZiJRcD0U3EHehryc"
}
]
}

→ We also get auto pagination handled data (see config/default.json).

→ There will be no body data passed for this. Passing token is must as Auth: Bearer token

B. Read Filtered Data:

TYPE: GET

URL: http://localhost:3030/vehicles?brand=<filter_parameter>

Eg: http://localhost:3030/vehicles?brand=TATA

Request: No Request call for GET Request

Response:

{
"total": 2,
"limit": 10,
"skip": 0,
"data": [
{
"brand": "TATA",
"model": "Grand i10",
"description": "Another success TATA product",
"price": 1500000,
"_id": "IiVncDRAXcNAKCD9"
},
{
"brand": "TATA",
"model": "i20",
"description": "Another success TATA product",
"price": 1500000,
"_id": "UT3Fn2rXqsVQ3DEN"
}
]
}

3. Update Vehicles:

→ You can just go to the NeDB file (i.e. data/vehicles.db)and update the data there

→ Update your data. Restart your Node server (i.e. yarn dev). All done

yarn dev or
npm run dev

After update:

→ With Read operation, we can see data is updated.

4. Delete Operation:

→ We need to go to the NeDB file of service (i.e. data/vehicles.db) and delete the required data as shown below screenshot.

→ Restart the node server

yarn dev or
npm run dev

Code Base :

Closing Thoughts:

We have created a secured REST application in very little time. Feathers saved us the work of a day or two. It supports multiple databases and the front end.

Other than REST, there are more features to feathers like sockets, multiple SSO authentication like Facebook, GitHub, etc.

Thank you for being till the end 🙌 . If you enjoyed this article or learned something new, support me by clicking the share button below to reach more people and/or give me a follow on Twitter to see some other tips, articles, and things I learn and share there.

--

--

Amir Mustafa
Geek Culture

JavaScript Specialist | Consultant | YouTuber 🎬. | AWS ☁️ | Docker 🐳 | Digital Nomad | Human. Connect with me on https://www.linkedin.com/in/amirmustafa1/