Series: Introduction to the MEAN Stack
- Part 1: Definition of the MEAN stack
- Part 2: Setup of the MEAN stack
- Part 3: Node.js
- Part 4: npm
- Part 5: Connect
- Part 6: Express
- Part 7: MongoDB
- Part 8: Mongoose
- Part 9: REST
- Part 10: Baucis
- Part 11: Bower
- Part 12: AngularJS
- Part 13: Restangular
In today’s article, we practically apply our theoretical knowledge about REST. To this end, I present Baucis , which quickly and easily generates a complete REST API.
Baucis is a framework that applies the concept of REST to Mongoose schemas and provides them as Express Middleware. First you should install your Baucis. The most recent version at the time of writing is 0.6.17:
1
|
$ npm install baucis
|
The use of Baucis is simple. You just need to assign a Mongoose scheme to a URL. Subsequently, the REST API is disclosed as middleware via Express. The code looks as follows:
1
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
var mongoose = require ( ‘mongoose’ ) ;
var baucis = require ( ‘baucis’ ) ; var express = require ( ‘express’ ) ; mongoose. connect ( ‘mongodb: // localhost: 27017 / todo-db’ ) ; var TodoSchema = new mongoose. Schema ( { baucis. rest ( { express ( ) |
As a schema, we define Todos consisting of a string called title
and a Boolean named completed
to indicate whether the Todo has been solved or not. The scheme is todo
called. As far as there is no change to the previous example except that we use a field with the type Boolean
which false
is automatic when creating a new Todos . Subsequently, the method transfers the CRUD functionality to the schema . This method accepts a configuration object whose value for the field must correspond exactly to the name of the corresponding schema. The value for the field is set automatically and plays a role at the later URL for the REST API. Since Baucis in this case frombaucis.rest
todo
singular
plural
todo
falsely todoes
makes the plural , I manually set the right plural form todos
.
The only middleware that uses the route prefix is used. The prefix is freely selectable, but is a frequently used prefix for accessing the REST API. The middleware now allows to control the previously exposed Mongoose schema via a REST API.baucis()
/api
/api
baucis()
baucis.rest
If you call http://127.0.0.1:1337/api/todos in the browser, you will get an empty array (“[]”). todos
the predefined plural form of todo
and the empty array means that the call was correct but no document todo
exists in the collection . With the command line tool cURL, you can quickly create a corresponding document. It is preinstalled on Mac OS X. The command is:
1
|
$ curl – X POST – H ‘Content type: application / json’ – d ‘{“title”: “REST learn”}’ http: // 127.0.0.1: 1337 / api / todos
|
If you now call http://127.0.0.1:1337/api/todos , you can see the newly created Todo:
The field _id
is automatically set by MongoDB and the field __v
is automatically set by Mongoose. _id
we will use immediately to manipulate the document; __v
can be ignored for this article series (see here for more information).
With the method PUT
and the method _id
we can update our document. Note that yours is _id
probably different!
1
|
$ curl – XPUT – H ‘Content type: application / json’ – d ‘{“completed”: “true”}’ http: // 127.0.0.1: 1337 / api / todos / 524989d90c67d4a135000001
|
Now the field completed
is true
set to. You can check it in the browser. With the following command the Todo can be completely deleted.
1
|
$ curl – X DELETE http: // 127.0.0.1: 1337 / api / todos / 524989d90c67d4a135000001
|
Congratulation! You have a working REST API. In the next chapter we will make the final preparations to develop a client for our web application with AngularJS.
0 Comments