Skip to content

Latest commit

 

History

History
executable file
·
84 lines (63 loc) · 3.77 KB

rest-api-server.md

File metadata and controls

executable file
·
84 lines (63 loc) · 3.77 KB

A REST API server

Our database connector will now function as a full fledged REST API server. We need only add a HTTP server to it.

HTTP servers. Feathers is currently tied into the popular HTTP server framework Express. Future versions will support multiple frameworks, starting with koa.

Working example

Implementing a REST API server

This is our previous example with the database method calls removed, and with an Express server added. import

The Express server common/expressServerConfig.js is configured as follows. import

The Express middleware common/expressMiddleware/index.js handles logging, pages not found, and general errors. import

Boilerplate. The server configuration and middleware are standard Express. They have little to do with Feathers other than to feed REST requests to it.

Running the server

We can now make REST API calls to the server.

In the previous example we created 3 user items and then printed the user file. We can now do the same thing, but using REST, with curl commands: import

First, start the server by running node ./examples/step/01/rest/1.js on one terminal.

Then run the curl commands with ./examples/step/01/rest/curl-requests.sh on another terminal.

Results

That console displays:

feathers-guide$ ./examples/step/01/rest/curl-requests.sh
POST Jane Doe
{"email":"jane.doe@gmail.com","password":"X2y6","role":"admin","_id":"sbkXV7LVkMhx1NyY"}
POST John Doe
{"email":"john.doe@gmail.com","password":"i6He","role":"user","_id":"uKhqOp4R4hABw9oO"}
POST Judy Doe
{"email":"judy.doe@gmail.com","password":"7jHw","role":"user","_id":"pvcmh9X2i9VZgqWJ"}
GET all users
[
 {"email":"judy.doe@gmail.com","password":"7jHw","role":"user","_id":"pvcmh9X2i9VZgqWJ"},
 {"email":"jane.doe@gmail.com","password":"X2y6","role":"admin","_id":"sbkXV7LVkMhx1NyY"},
 {"email":"john.doe@gmail.com","password":"i6He","role":"user","_id":"uKhqOp4R4hABw9oO"}
]

Feathers. REST API calls are automatically converted into Feathers database method calls like the users.create() and users.find() methods we used in the previous example. How's that for convenience?

Is anything wrong, unclear, missing?

Leave a comment.