GoRapid Blog

Stories from people who build @GoRapid

Install Feathers for creating the Simple Rest APIs Application

25 September: Vaibhav Sharma

Feathers is a lightweight web-framework for creating real-time applications and REST APIs using JavaScript or TypeScript.

We can create the REST APIs in very simple steps. Feathers JS provide CLI tool for creating the application, services and for hooks.

We don't need to write the services for it manually . If you are creating the files and the directory for services on your own, you must be using the CLI tools for creating the application, services, hooks and the middle ware. Feathers CLI tool can be installed by this command

npm install @feathersjs/cli -g 

Creating the Feathers Application

You can create the application using CLI with this Feathers command. Application can be in JS or TS it is all about your preference. GoRapid prefers TS for OOPs concepts and for eliminating the short comings of the JS. We also recommend to use Type script.

feathers generate app 

This command will ask many things for creating the application. They are listed below

1 Do you want to use JavaScript or TypeScript? TypeScript
2 Project name tutorial
3 Description tutorials
4 What folder should the source files live in? src
5 Which package manager are you using (has to be installed globally)? Yarn
6 What type of API are you making? REST, Realtime via Socket.io
7 Which testing framework do you prefer? Jest
8 This app uses authentication Yes
9 What authentication strategies do you want to use? (See API docs for all 180+ supported oAuth providers) Username + Password (Local)
10 What is the name of the user (entity) service? users
11 What kind of service is it? KnexJS
12 Which database are you connecting to? MySQL (MariaDB)
13 What is the database connection string? mysql://root:@localhost:3306/tutorial 

  1. What you prefer (TS ot JS), GoRapid uses TS.
  2. Project name, as per your choice
  3. Description of the project
  4. Directory for the scr which will be converted into the JS code
  5. What type of API you want. REST, Real time via socket.io or via primus.
  6. Package manager you prefer
  7. Test frame work you prefer. We use jest
  8. Application for the Simple rest or it is required authentication
  9. What authentication you are required (Github, Google, Facebook or Local)
  10. User entity can be changed (Like students, Shopkeepers)
  11. What database do you want to use (mongo, mysql, cassandra, postgress)
  12. Your database connection string

Now you have successfully created the application.

Connect your application to Database

In the config directory there are configuration files for the different environment. In the default.json there are default settings. Now change the mysql settings for the connection. We have already created the database named as feather_tutorials. Mysql connection be like in the config file as

{
  "host": "localhost",
  "port": 3030,
  "public": "../public/",
  "paginate": {
    "default": 10,
    "max": 50
  },
  "authentication": {
    "entity": "user",
    "service": "users",
    "secret": "L5+bLPfFdT1xtLyxIazymW36C5c=",
    "authStrategies": [
      "jwt",
      "local"
    ],
    "jwtOptions": {
      "header": {
        "typ": "access"
      },
      "audience": "https://yourdomain.com",
      "issuer": "feathers",
      "algorithm": "HS256",
      "expiresIn": "1d"
    },
    "local": {
      "usernameField": "email",
      "passwordField": "password"
    }
  },
  "tutorial": {
    "client": "mysql2",
    "connection": {
      "host": "localhost",
      "port": 3306,
      "user": "tutorial",
      "password": "tutorial",
      "database": "tutorial",
      "timezone": "+00:00"
    }
  }
} 

There are some changes in the files. knex.js ( database name has to be changed ). Now run the command for starting the application.

yarn dev 

Application is started now and you can create user using this request.

curl --location --request POST 'localhost:3030/users' \
--header 'Content-Type: application/json' \
--data-raw '{
    "email":"vaibhav.sharma@gorapid.io",
    "password":"vaibhav"
}' 

User has been added to the application. Now it can be authenticated using this CURL after authentication we can create another services and play with them.

curl --location --request POST 'localhost:3030/authentication' \
--header 'Content-Type: application/json' \
--data-raw '{
    "email":"vaibhav.sharma@gorapid.io",
    "strategy":"local",
    "password":"vaibhav"
}' 

Congratulations you have completed first step to create the Rest API with very simple commands. Be with GoRapid for creating services in the next chapter. GoRapid will cover Knex JS migrations with Feathers JS. Thank you for Reading.

Install Feathers for creating the Simple Rest APIs Application

25 September: Vaibhav Sharma

Feathers is a lightweight web-framework for creating real-time applications and REST APIs using JavaScript or TypeScript.

We can create the REST APIs in very simple steps. Feathers JS provide CLI tool for creating the application, services and for hooks.

We don't need to write the services for it manually . If you are creating the files and the directory for services on your own, you must be using the CLI tools for creating the application, services, hooks and the middle ware. Feathers CLI tool can be installed by this command

npm install @feathersjs/cli -g 

Creating the Feathers Application

You can create the application using CLI with this Feathers command. Application can be in JS or TS it is all about your preference. GoRapid prefers TS for OOPs concepts and for eliminating the short comings of the JS. We also recommend to use Type script.

feathers generate app 

This command will ask many things for creating the application. They are listed below

1 Do you want to use JavaScript or TypeScript? TypeScript
2 Project name tutorial
3 Description tutorials
4 What folder should the source files live in? src
5 Which package manager are you using (has to be installed globally)? Yarn
6 What type of API are you making? REST, Realtime via Socket.io
7 Which testing framework do you prefer? Jest
8 This app uses authentication Yes
9 What authentication strategies do you want to use? (See API docs for all 180+ supported oAuth providers) Username + Password (Local)
10 What is the name of the user (entity) service? users
11 What kind of service is it? KnexJS
12 Which database are you connecting to? MySQL (MariaDB)
13 What is the database connection string? mysql://root:@localhost:3306/tutorial 

  1. What you prefer (TS ot JS), GoRapid uses TS.
  2. Project name, as per your choice
  3. Description of the project
  4. Directory for the scr which will be converted into the JS code
  5. What type of API you want. REST, Real time via socket.io or via primus.
  6. Package manager you prefer
  7. Test frame work you prefer. We use jest
  8. Application for the Simple rest or it is required authentication
  9. What authentication you are required (Github, Google, Facebook or Local)
  10. User entity can be changed (Like students, Shopkeepers)
  11. What database do you want to use (mongo, mysql, cassandra, postgress)
  12. Your database connection string

Now you have successfully created the application.

Connect your application to Database

In the config directory there are configuration files for the different environment. In the default.json there are default settings. Now change the mysql settings for the connection. We have already created the database named as feather_tutorials. Mysql connection be like in the config file as

{
  "host": "localhost",
  "port": 3030,
  "public": "../public/",
  "paginate": {
    "default": 10,
    "max": 50
  },
  "authentication": {
    "entity": "user",
    "service": "users",
    "secret": "L5+bLPfFdT1xtLyxIazymW36C5c=",
    "authStrategies": [
      "jwt",
      "local"
    ],
    "jwtOptions": {
      "header": {
        "typ": "access"
      },
      "audience": "https://yourdomain.com",
      "issuer": "feathers",
      "algorithm": "HS256",
      "expiresIn": "1d"
    },
    "local": {
      "usernameField": "email",
      "passwordField": "password"
    }
  },
  "tutorial": {
    "client": "mysql2",
    "connection": {
      "host": "localhost",
      "port": 3306,
      "user": "tutorial",
      "password": "tutorial",
      "database": "tutorial",
      "timezone": "+00:00"
    }
  }
} 

There are some changes in the files. knex.js ( database name has to be changed ). Now run the command for starting the application.

yarn dev 

Application is started now and you can create user using this request.

curl --location --request POST 'localhost:3030/users' \
--header 'Content-Type: application/json' \
--data-raw '{
    "email":"vaibhav.sharma@gorapid.io",
    "password":"vaibhav"
}' 

User has been added to the application. Now it can be authenticated using this CURL after authentication we can create another services and play with them.

curl --location --request POST 'localhost:3030/authentication' \
--header 'Content-Type: application/json' \
--data-raw '{
    "email":"vaibhav.sharma@gorapid.io",
    "strategy":"local",
    "password":"vaibhav"
}' 

Congratulations you have completed first step to create the Rest API with very simple commands. Be with GoRapid for creating services in the next chapter. GoRapid will cover Knex JS migrations with Feathers JS. Thank you for Reading.

Comments 0