Permalink
Browse files

First commit

  • Loading branch information...
1 parent d4eb729 commit 138fd389feba881d0410e8a1be12724c7b472270 @itswadesh committed Sep 1, 2015
View
@@ -0,0 +1 @@
+/node_modules
View
@@ -1 +1,13 @@
-# simple-restful-api-angularjs
+## Creating ReSTful API using NodeJS and consuming in AngularJS
+
+Create ReST API using NodeJS, MongoDB and consuming in a application created uisng AngularJS
+
+#### Requirements
+Install the following 2 softwares
+* Node http://nodejs.org/
+* MongoDB https://www.mongodb.org/
+
+#### Install
+To start the application, run the following 2 commands
+* npm install
+* node server.js
View
@@ -0,0 +1,14 @@
+
+// Dependencies
+var restful = require('node-restful');
+var mongoose = restful.mongoose;
+
+// Schema
+var productSchema = new mongoose.Schema({
+ name: String,
+ sku: String,
+ price: Number
+});
+
+// Return model
+module.exports = restful.model('Products', productSchema);
View
@@ -0,0 +1,14 @@
+{
+ "name": "ReST-API",
+ "version": "1.0.0",
+ "description": "A simple RESTFul API created using nodejs that talks to Mongodb database",
+ "main": "index.js",
+ "author": "codenx",
+ "license": "ISC",
+ "dependencies": {
+ "body-parser": "^1.13.3",
+ "express": "^4.13.3",
+ "mongoose": "^4.1.3",
+ "node-restful": "^0.2.2"
+ }
+}
View
@@ -0,0 +1,52 @@
+var app = angular.module('myApp', ['ngResource', 'ngProgress', 'ngAnimate', 'toaster']);
+app.config(function ($httpProvider) {
+ $httpProvider.interceptors.push('myHttpInterceptor');
+});
+
+// Handle http server errors
+app.factory('myHttpInterceptor', function ($q,toaster) {
+ return {
+ responseError: function (response) {
+ console.log(response);
+ if(response.data){
+ if(response.data.message)
+ toaster.error("Error: ", response.data.message);
+ else
+ toaster.error("Error: ", response.data);
+ }
+ return $q.reject(response);
+ }
+ };
+});
+
+// Showing loading indicator on top while data is requested from database
+app.directive('loading', ['$http', 'ngProgress', function ($http, ngProgress)
+{
+ return {
+ restrict: 'A',
+ link: function (scope, elm, attrs)
+ {
+ scope.isLoading = function () {
+ return $http.pendingRequests.length > 0;
+ };
+
+ scope.$watch(scope.isLoading, function (v)
+ {
+ if(v){
+ ngProgress.start();
+ }else{
+ ngProgress.complete();
+ }
+ });
+ }
+ };
+}]);
+
+// Create a resource factory to access products table from database
+app.factory('Product', function($resource) {
+ return $resource('/api/products/:id', { id: '@_id' }, {
+ update: { // We need to define this method manually as it is not provided with ng-resource
+ method: 'PUT'
+ }
+ });
+});
@@ -0,0 +1,37 @@
+app.controller('ProductsCtrl', function($scope, Product, ngProgress, toaster) {
+
+$scope.product = new Product();
+
+var refresh = function() {
+ $scope.products = Product.query();
+ $scope.product ="";
+}
+refresh();
+
+$scope.add = function(product) {
+ Product.save(product,function(product){
+ refresh();
+ });
+};
+
+$scope.update = function(product) {
+ product.$update(function(){
+ refresh();
+ });
+};
+
+$scope.remove = function(product) {
+ product.$delete(function(){
+ refresh();
+ });
+};
+
+$scope.edit = function(id) {
+ $scope.product = Product.get({ id: id });
+};
+
+$scope.deselect = function() {
+ $scope.product = "";
+}
+
+})
Oops, something went wrong.
@@ -0,0 +1,28 @@
+/* Styling for the ngProgress itself */
+#ngProgress {
+ margin: 0;
+ padding: 0;
+ z-index: 99998;
+ background-color: green;
+ color: green;
+ box-shadow: 0 0 10px 0; /* Inherits the font color */
+ height: 2px;
+ opacity: 0;
+
+ /* Add CSS3 styles for transition smoothing */
+ -webkit-transition: all 0.5s ease-in-out;
+ -moz-transition: all 0.5s ease-in-out;
+ -o-transition: all 0.5s ease-in-out;
+ transition: all 0.5s ease-in-out;
+}
+
+/* Styling for the ngProgress-container */
+#ngProgress-container {
+ position: fixed;
+ margin: 0;
+ padding: 0;
+ top: 0;
+ left: 0;
+ right: 0;
+ z-index: 99999;
+}
Oops, something went wrong.

0 comments on commit 138fd38

Please sign in to comment.