Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to re-use code between AngularJS client and Node.js server [closed]

Tags:

What are the best-practices in order to re-use/share code between an AngularJS client and a Node.js server?

I implemented an AngularJS application. Now I need to implement a RESTful-server providing the client with data. Some client-side angular services could be re-used on server, as for example third-party restful-clients to Facebook/Google/Twitter, which use intensively the angular dependency injection and which are dependent on $http, $q and many other services.

Ideally, as I really like the dependency injection framework included in AngularJS, I would find very nice to have a kind of server-framework based on AngularJS. A server-framework that includes the dependency injection framework and all angular-services that are not related to UI, and adding required server-side functionality like routing and authentication. But unfortunately, I didn't find any solution going that way. (Please tell me if such a framework exists!)

So, what would be an alternative, in order to at least enable code re-use between the client and the server? Particularly enabling code re-use for code depending on $http, $q and other AngularJS services included in the angular framework and angular-third-parties (like angular-cache).

like image 824
jeromerg Avatar asked Mar 31 '14 11:03

jeromerg


People also ask

Can we use Angular and NodeJS together?

NodeJS takes part in loading the AngularJS application with all the dependencies, such as CSS files and JS files in the browser. For loading all the assets of Angular and accepting all the API calls from the AngularJS applications, NodeJS is generally used as a web server.

What is NodeJS server side JavaScript used for?

Node. js is a runtime environment to allow JavaScript to not only be run in the browser, but also on the server (or almost any environment, really). That also expanded the types of applications that could be built with the language since it wasn't tied to only the client-side anymore.


1 Answers

Using angular in non-browser environment

Ben Clinkinbeard prepared angular distribution as a commonJS module (HERE), that can run in a non-browser environment:

AngularJS compiled with jsdom and provided as a CommonJS module. Intended for testing AngularJS code without depending on a browser.


If you want to be more cherry-picking you probably should wait for angular 2.0

Sharing code between client-side and server-side

And again... Ben Clinkinbeard. In THIS TALK he describes how to make use of browserify in an angular project. One cool feature of this approach is that you can declare your functions/objects as separate entities that are not coupled with angular anyhow; so they can be re-used in different contexts as well.

An example of it:

app.js

var app = angular.module('someModule',[]); ... app.factory('someService', require('./some/path.js')); ... 

./some/path.js

module.exports = function(dep1, dep2){   ...   return {      ...   } } module.exports.$inject['dep1', 'dep2']; // for minification; 
like image 67
artur grzesiak Avatar answered Oct 01 '22 08:10

artur grzesiak