Giter VIP home page Giter VIP logo

node-angular-resource's Introduction

node-angular-resource Build Status

AngularJS $resource bindings for express in node

Description

angular-resource is a node module that simplifies the use of angularjs $resource factory by creating the default set of resource actions for get, save, query, remove and delete.

It is best described by example. Suppose you have an angularjs service defined as follows:

var myServices = angular.module('myServices', ['ngResource'])

myServices.factory('Task', function($resource) {
	return $resource('api/tasks/:id', { id: "@_id" });
});

The myServices Task will, by default, now support get, save, query, remove and delete. The required endpoints can simply be routed via express by using the angular-resource module:

First create a task.js with the following details:

var task = { };

task.get = function(req, res) {
	res.json({});
};

task.save = function(req, res) {
	res.send(200);
};

task.query = function(req, res) {
	res.json([]);
};

task.remove = function(req, res) {
	res.send(200);
};

module.exports = task;

and then use angular-resource to bind it into express.

var angularResource = require('angular-resource'),
		express = require('express');

var app = express();

angularResource(app, '/api/1', 'task');

app.listen(3000);

This will bind the required endpoints through to task.js. Note that both 'delete' and 'remove' are routed to the 'remove' method.

If you wish to use middleware, then define the binding as follows:

var middleware = function(req, res, next) {
	return next(req, res);
};

angularResource(app, '/api/1', 'task', middleware);

If you don't want to support all of the default $resource actions, then just omit them from the object. In the example above, if you don't want to support remove then just define task.js as follows:

var task = { };

task.get = function(req, res) {
	res.json({});
};

task.save = function(req, res) {
	res.send(200);
};

task.query = function(req, res) {
	res.json([]);
};

module.exports = task;

You can now create includes for all of the $resource objects your angularjs services require and bind them in the same way.

node-angular-resource's People

Contributors

roylines avatar

Watchers

 avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.