a node.js simple REST app using ejs and mongodb
This example is based on: http://coenraets.org/blog/2012/10/creating-a-rest-api-using-node-js-express-and-mongodb/
- CRUD - Create, Read, Update and Delete
- REST - REpresentational State Transfer
- DAO - Data Access Object
- node.js
- express
- ejs (view engine)
- mongodb
- Presentation - HTML and CSS. The HTML files contain only static data and controls, NO format and NO logic.
- Presentation logic - Javascript and jQuery.
- Business logic interface - REST services. Presentation logic call REST services (using jQuery), which produces JSON data.
- Business logic - Logic or service classes, separated from REST facades. SOA style decoupling.
- Data logic - DAO classes.
- execute the following commands from the project library:
-
$ npm install -g express // install express globally
-
$ express --sessions --ejs // create express application skeleton (with EJS and Session)
-
$ npm install -d // install all dependencies
-
$ npm install mongodb
/
|- package.json
|- app.js
/ public
/ javascripts
/ images
/ stylesheets
|- style.css
/ routes
|- index.js
|- user.js
/ views
|- index.ejs
/
|- package.json
|- app.js
/ lib
- products.js [Data Logic (DAO)]
/ public
/ javascripts [Presentation Logic]
/ external
/ views_cb
|- index_cb.js //code-behind for index.ejs (you will find jQuery ajax requests here)
/ images
/ stylesheets [Presentation]
|- style.css
/ routes
|- index.js
/ views [Presentation]
|- index.ejs
|- layout.ejs
|- products.ejs
can be done using cURL (command line utility for transferring data with URL syntax)
- GET all products: curl -i -X GET http://localhost:3000/products
- GET product with a specific id: curl -i -X GET http://localhost:3000/products/5069b47aa892630aae000007
- DELETE product with a specific id: curl -i -X DELETE http://localhost:3000/products/5069b47aa892630aae000007
- POST (add) new product: curl -i -X POST -H 'Content-Type: application/json' -d "name=iPhone5&company=apple" http://localhost:3000/products
- PUT (modify) product with a specific id: curl -i -X PUT -H 'Content-Type: application/json' -d "name=Galaxy3&company=samsung" http://localhost:3000/products/5069b47aa892630aae000007