

How to Build REST API Using Node Js Feathers Framework
source link: https://dev.to/devpool3000/how-to-build-rest-api-using-node-js-feathers-framework-4pli
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.


How to Build REST API Using Node Js Feathers Framework
Jul 26
・4 min read
This article will create a simple REST API vehicle system with full authentication and CRUD (create, read, update, delete) capabilities.
We will be creating a simple vehicle API support.
Our Use Cases:
- As a user, I should be able to register.
- As a user, I should be able to login.
As a user, I should be able to perform any CRUD operations on the vehicle data.
- create
- update
- delete
Project Setup
mkdir vehicle-api
cd vehicle-api
feathers g app
As you're answering the above questions, you'll notice that you can create your application using TypeScript and not JavaScript, or we can select any other database type.
For simplicity, I went with yarn, JavaScript, and NeDB, but if you're planning to use npm, TypeScript, or Postgres/MongoDB, you can do so.
Next, open your project with an editor of your choosing (I'm personally going with VS Code)
To run the application, run the appropriate package manager command that you've selected. In this case:
yarn dev
and you should be able to see this page in your browser:
Congratulations!
You've set up your first Feathers application, and here is the tree structure of our project:
You can find more information how what do this files do in here: https://docs.feathersjs.com/guides/basics/generator.html#the-generated-files
Notice how under 1 minute, we've generated an application with a full authentication implementation and our first user service has full CRUD capabilities.
So how do we communicate with our backend if we don't have a frontend?
Whenever we work on the API, engineers would use a tool called Insomnia or Postgres (in this example, I'll be using insomnia). Overall it will enable us to send and get a response from our API without running the frontend.
What we want to do next is to create "register" and "login" requests in our insomnia workspace.
In my workspace, I'll be creating a new folder, "Authentication," that will contain "register" and "login" requests.
Register
After sending the request, we should see the following:
Login
After sending the request, we should see the following:
If you see the same thing, then your authentication is set up correctly!
Since the basics of our applications are set up and running, it's time to create our vehicle service.
To do that, run the following command and answer all of the questions in the same way as we did it right from the beginning:
feathers g service
And if you look under the services/model folder, we now have a vehicle implementation that we can perform entire CRUD operations on.
At this point, our REST API is fully ready to take in some data and perform the entire CRUD operation on our new vehicle service!
Let's create a few records to demonstrate that.
Oh no, what happened here!? We're getting an error message "Not authenticated."
That's the expected behavior because our system is saying that "you're trying to access an endpoint when you are not authenticated." Translation, not logged in.
So let's send the request with the generated token from before and try to resent the request
At this point, we can now save a record into our DB.
Let's create few more records and get all vehicles:
At this point, our Update and Delete will look very similar.
Additional Recourses:
If this was your first RESTful API build, unfortunately, I didn't explain models, why we named our folders "Authentication" or "vehicles" in our insomnia, and what some of the things are. Thankfully, I do have a video where I give more information for beginners as I'm building out the API:
Feathers Documentation:
https://docs.feathersjs.com/guides/basics/generator.html
Conclusion
We just created our first RESTfull API using FeatherJs framework WooHoo! I hope this tutorial was helpful for some of you. We did many things, and it could be a lot at first and could be confusing at the beginning. But to be honest, once you master this, you will realize that we have created an amazing backend with many complex pieces, and we can create all of it unless 2 minutes. So big congrats to you on getting to the end of this tutorial!
If you have any questions, feel free to leave a comment or you can find me on my YouTube channel DevPool
Recommend
-
25
Michael Feathers finds errors fascinating, but acknowledges that most developers don't spend a lot of time focusing on them. He also thinks most error handling is kind of giving up. Although best known for his book,
-
65
Michael Feathers因其著作《高效操作遗留代码》( Working Effectively With Legacy Code )一书而广为人知。他发现错误中存在着一些值得关注之处,但...
-
17
Announcing Feathers-Vuex 3.14.0
-
26
-
10
Introducing Feathers 4: A framework for real-time apps and REST APIsToday I’m happy to share the release of Feathers 4 (Crow), a framework for real-time applications and REST APIs with JavaScript and TypeScript. ? ?
-
10
Feathers spring 2019 news and v4 beta testA lot has been happening under the hood the past few months ? and I’m happy to share the pre-release of Feathers v4 (Crow) and some great articles, modules and videos ?
-
16
Node.js Postgresql tutorial: Build a simple REST API with Express step-by-step01-Jan-2021 | (about a 22 minute read)Node.js can be used efficiently with relational databases like PostgreSQL. In this post about Node.js Pos...
-
9
Build a REST API with Node.js and ExpressWhat is REST? What is an API? Learn how to build a simple RESTful API with Node.js and ExpressPhoto by
-
10
Introducing Feathers 5 — The API and real-time application framework
-
12
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK