The goal is to make a REST Full web service example under NodeJS
, ExpressJs
and Mongoose
(MongoDB) to effectively manage a category tree localized in several languages.
I assume you have already installed node and the various extensions mentioned above.
The project can be downloaded from Git here.
This first version allows you to:
- Insert a category
- Insert a translation to a category
- Delete a category
- Delete a translation to a category
- Get a category by id
- Retrieve the list of categories down to a category
- Retrieve the list of categories down to a category and the category itself
- Retrieve the list of categories up to a category
- Retrieve the list of categories up to a category and the category itself
- Get the root category
- Get the children of a category
- Move a category (and or not the children) in the tree
Remains to be done:
- Find a category by name
- Update the default or localize title of a category
- Get a global tree for a category node
Please remember that this is my first NodeJS project, so...
If you improve or expand the project, let me know.
Linux do the following:
$ Node app.js
The server will be start on port 3000.
Start a terminal and use Curl to call the web service and consume it. Once launched the terminal type the following command:
$ alias API_CATEGORY="curl -H 'Content-Type: application/json' -H 'Accept: application/json'"
This is just a shortcut (containing the HTTP headers) that will be used later
If a web service call fails for some reason or another (update did not work, use a non-existent category, etc ...), one of the following form json is returned:
{"code":404,"status":"Not Found","message":"error message"}
The HTTP status code is tailored to the circumstances:
404
: element not found.500
: other error not listed
http://localhost:3000/category/:categoryid
Retrieve a single category item by its _id
categoryid
: Category _id (eg: 4f1d76349fb183ec18000004)
lang
: Language code x(e.g. fr), this option allows you to retrieve the localize title of a category, i not specify the default title is retrieve.
$ API_CATEGORY -X GET -d '' 'http://localhost:3000/category/4f1d76349fb183ec18000004'
or
$ API_CATEGORY -X GET -d '' 'http://localhost:3000/category/4f1d76349fb183ec18000004/?lang=fr'
All json fields are in Pascal case.
{"Success":true,"Result":{"Title":"My wonderful title","_id":"4f1d76349fb183ec18000004"}}
#####Route
http://localhost:3000/category/:categoryid/children
Retrieves a list of child categories of a given category by its _id
Categoryid
: _id d'une catégorie (par exemple 4f1d76199fb183ec18000002)
lang
: Language code x(e.g. fr), this option allows you to retrieve the localize title of a category, i not specify the default title is retrieve.
page
: Page index (e.g.: 0 for the first page).
limit
: Number of records to retrieve (e.g.: 10)
In summary if the values for page = 3 & limit = 10, the first 30 results will be skipped and we get the following 10. Convenient for paging ...
API_CATEGORY -X GET -d '' 'http://localhost:3000/category/4f1d76349fb183ec18000004/children/?page=0&limit=2
{
"Success":true,
"Page":"0",
"Count":2,
"Total":4,
"Result":
[
{"Title":"California","_id":"4f1d7ca6dd5890fc18000003"}, {"Title":"Wisconsin","_id":"4f1d76879fb183ec18000006"}
]
}
With :
Page : The page that was requested.
Count : The number of records returned (may be less than the number requested).
Total : The total number of records in database for this request.
Result : Array of listitems.
http://localhost:3000/category
Inserts a new category.
title
: Default title of the category.
parentcategoryid
: Parent category id (e.g.: 4f1d76349fb183ec18000004)
position
: position a the new category relative to the parent category.
fc : **f**irst **c**hild .
lc : **l**ast **c**hild
fs : **f**irst **s**ibbling
ls : **l**ast **s**ibbling
p : **p**arent
$ API_CATEGORY -X POST -d '{"title":"test","parentcategoryid":"4f1d76349fb183ec18000004","position":"fc"}' 'http://localhost:3000/category/'
is succeeded return the new category id.
{"Success":true,"Result":{"_id":"4f21d53720636dc729000015"}}
http://localhost:3000/category/:categoryid
categoryid
: Id of the category winch will be deleted.
recursive
: boolean indicates how the category will be deleted.
true: Delete category and all its descendants.
false: Deletes only the category and then step back all his descendants.
$ API_CATEGORY -X DELETE -d '{"recursive":"true"}' 'http://localhost:3000/category/4f21d53720636dc729000015'
{"success":true,"deleted":1}
Follow @wilfridb on Twitter for updated news.