fbpx

Series: Introduction to the MEAN Stack

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 {
title { type : String default  ,
completed { type : Boolean default false 

mongoose. model ‘todo’ , TodoSchema 

baucis. rest {
singular ‘todo’ ,
plural ‘todos’ 

express )
use ‘/ api’ , baucis )
list1337 ‘127.0.0.1’ ;

As a schema, we define Todos consisting of a string called title and a Boolean named completedto 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  Booleanwhich falseis 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.resttodosingularpluraltodo 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/apibaucis()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 – ‘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:

REST API in use

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 – ‘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

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.