In my last post, I explained the fundamental concepts that one needs to understand in order to leverage the power of Feathers, but in this post, rather than having a high-level overview of how the framework works, we’ll be creating a very simple Feathers application that can say “Hello” to the world.
This app will consist of a single service that says hello to anyone who writes their name in the commandline, and it remembers their names. It can also say hello to everyone it knows if asked to.
Before we start building this amazing application, make sure Node.js is installed on your machine, if not, you can download it from here. I’m using Node version 8.11.2 and npm version 5.6.0 for this app.
Let’s get started by creating a directory named feathers-hello-world
to put our project in. From your terminal, create the directory and change your working directory to it, like so:
From here we can initialize our project with :
Which will create a package.json
file, which contains details about our project, including declarations of the project’s dependencies.
Now let’s install our only dependency, @feathersjs/feathers
:
When all is done, your package.json
file should look like this:
Now let’s create an index.js
file to write all of our code in:
Now let’s initialize our application inside index.js
like so:
In the first line, we import FeathersJS in order to initialize our _app_
object using it in the second line.
Now let us implement our HelloService
, which implements only two service methods: create, and find.
Let me explain what’s going on:
- In the constructor, we initialize the
names
array, which is the array that we’ll store people’s names in. - The
create
method is one of the service methods, and it is responsible for creating new records of people we may want to greet again in the future.
Thecreate
method accepts two parameters:data
, which is an object containing the data we will be storing, andparams
, an optional parameter that can contain extra data we can use in the process of creating the record.
Not only does thecreate
method store the name of the new visitor, but it also returns a personal greeting for that person.
Note: A service method must return a JavaScript Promise, or it must be an asynchronous function.
- The
find
method is another one of the Feathers service methods, it is responsible for retrieving all greetings the application had ever greeted.
Thefind
method accepts one parameter:params
, which can contain information that can be used in searching the records for more specific results. - The
helloTo
method is simply a helper method that is used by both service methods to turn people’s names into greetings. - In the first part of this series, I briefly mentioned the
app
object, and how it ties our application together, and in the last line we see how a service is registered using theuse()
method of theapp
object. Every service in our application has to be registered in the same manner.
The first argument ofapp.use()
is the path to the service, which is the string that you’ll be using to get a hold of the service through theapp.service()
method. The second argument is a new instance of the service you want to register, or simply an object that implements one or more of the service methods.
Our service is now ready for use. Now let us write a little bit of code to allow the user to interact with the app through the commandline:
Notice how we used app.service('hello')
to obtain a reference to the HelloService
, this is the method you should use to reach any service in your application.
Now if you run the app with the command:
The app will say hello to you if you type in your name and hit Enter, but not just you, it can say hello to anyone who tells it their name!, friends, family, really anyone you introduce it to!, and if you want it to say hello to everyone it knows, you can type “everyone” and hit Enter, how cool is that?…… not so cool I’m afraid. This is how index.js
should look like now:
What is cool though, is that you can turn this pp into a RESTful API with one command and 6 lines of code. Start by installing @feathersjs/express
:
Now change your index.js
file to look like this:
I’ll spare you the details of RESTful Feathers APIs until my next post, but for now, it suffices to say that calling service methods using HTTP is very similar to the way we called them locally, where you would use HTTP GET to call the find method, and POST to call the create method.
When running node index.js
, you should see the following printed out to your terminal:
Hello World app is running on http://localhost:3000
Using a tool like CURL or Postman, you can send an HTTP POST request to http://localhost:3000/hello
withe the JSON body: { "name": "John Doe" }
and the server would respond with a friendly “Hello, John Doe!”. If you send an HTTP GET request to the same URL, you’ll get an array of greetings for all the names that you’ve posted to the hello service.
Conclusion
In this post, we’ve built a very basic Feathers application that consists of a single service, which allowed us to see two of the service methods in action, and we saw how easy it is to use said service using REST. In the next part, we’ll be building a more practical, persistent RESTful API, which will give us the chance to look at how hooks are implemented, and how easy Feathers makes accessing your database with database adapters.