
Read more »

In this post I am going to cover a very basic CRUD operations using MEAN (MongoDB, Express, Angular and NodeJS) stack. You can use this example to kick start your project using MEAN stack.

Before I begin, I am assuming that the reader of this post has basic understanding of AngularJS, MongoDB, NodeJS, jQuery and other client side technologies like jQuery, HTML 5, CSS 3, etc.
Most of the commands, environment I have used is for Windows operating system, but there’s not much difference if you are using MacOS or Linux operating system, and moreover all the technologies used in this example are platform independent.
Follow the URL in their respective sites to get detailed information on download and installation instructions. You can get the installers of all the OS which is supported by these framework and their installation instructions in these links.
Solution Structure: In this example I am using a person database and the only function of this application would be to Create-Read-Update-Delete a person record. To achieve this I have created a simple solution structure, using the following structure.
Here I am using app folder for most of my application code, this folder contains angular controller, service and views. I am keeping my node modules very simple just to expose API’s which will be consumed by AngularJS services. I have covered most of the server side node modules in just one file called server.js I will try to cover specially more complex node module/architecture in my next post.
Download and installation instructions: For this example my application is using following node-modules, to install these modules I am using node package manager using GitBash.
I am using my node.js for middleware, to setup my middleware first I have created my server in node.js
Here in line 1 and 2 above I have imported express module and created a instance of express, and in line 3 I have used listen function of express to create new server on port 3000. With this example my server is listening on port 3000. When I run this in GitBash I will get the following message.
I still need to add few more modules to my server.js which will help the application to connect to my mongodb server, configure the application working directories. But before I do that let me first start creating GET/POST/PUT and DELETE API’s in server.js which can communicate with my Client module in AngularJS
In the above code I have create a blueprint of my middleware in server.js
| Node JS API Controller | Description |
| /persons | Get All Persons |
| /person/:id | Get single person by person Id |
| /addPerson | Add new person |
| /deletePerson/:id | Delete single person by id |
| /updatePerson | Update single person |
in the first few lines of my code I have created the instances of express and configure my application to use my root directory of the application as a working folder. Now lets run the server and check the output in web browser.
In this example I have created stubs for Get by Id, GetAll, Update, and Delete person request. To test the code start the node server, though initially you can see the same output what I have provided earlier. But when you enter the URL’s in the browser with the URL’s on which I have created my API’s you can see the outputs in the console.
I have provided example of GET requests. I will write Angular modules to test POST/PUT and DELETE API’s
GET All (localhost/persons)
GET By ID (localhost/person/1)
I am using simple MVC model in AngularJS Client, but you can fork or clone my Git repository to extend this solution. This example assume that you have basic understanding of Angular JS. To start with lets create a simple router which redirects my application to the home page of my person page.
Angular JS Routing
Here in the example above, I have mapped the paths /person and /person/:PersonId with the PersonCtrl and PersonAddressCtrl controller respectively and corresponding view as app/views/person.html and app/views/persondetail.html views respectively. Which means when I hit the URL: http://localhost:3000/person, this will go to the person.html and mapped to the PersonCtrl.js controller and when I hit a single person using URL: http://localhost:3000/person/1 out of the list using personId my routing engine will redirect the users to persondetail.html using PersonAddressCtrl.js
Angular Controllers – PersonCtrl
onError : This is going to be my common error module, this action method is used only for the purpose of displaying the error when something goes wrong during the whole communicate process wither at the server level or at the client level.
refresh : This will be my default get all person action method, which is used to load all the person data from mongodb database via nodejs. Here in this code I am calling the corresponding API ‘/persons’ which is created in nodejs and express to populate all the persons from mongodb. Once the data is returned from mongodb I have created a callback function onPersonGetCompleted event, which is used to populate the person data in the scope of the current module. refresh() is called as soon as person.html my default page is loaded in the application or every time we need to refresh my data from mongodb.
searchPerson : This action method will be called when I want to view a single person based on person id, for example during opening the update form for the selected person. This method is associated with the callback function onGetByIdCompleted, which populates the scope with a single person result from mongodb.
addPerson : As name suggest, this action method is used to add a new person, and the associated call back function to this is onAddPersonCompleted, which refresh the data once add is completed to reflect the uptodate information on the Person default page.
deletePerson : As the name suggest, only purpose of this action method is used to delete the selected person. This calls '/deletePerson/:id' API of nodejs and once the call returns it uses its callback function onPersonDeleteCompleted to refresh the update data in the HTML page person.html
updatePerson : This action updates the person information in mongodb. This calls the ‘/updatePerson’ API in nodejs. Once the update completed, it calls the callback function onUpdatePersonCompleted action in Angular to refresh the updated data in the Person page.
Let's put everything together to see my completed PersonCtrl.js, next I am going to show how this is going to integrate with the View and the final step will be to write the body of my API controller which will be used to transact with mongodb.
Now lets revisit the API controller, the skleton of which I had created in the Step 4 above. Here I am going to write the body of my GET/POST/PUT and DELETE methods which will be used to transact with mongodb document database. In the chart below I have shown the NodeJS express API and corrosponding mongodb API against that which will be called to complete the transactions.
| Action | NodeJS Express API | MongoDB API |
| Get All Persons | /persons | db.Persons.find() |
| Get Person by Id | /person/:id | db.Persons.findOne() |
| Add new Person | /addPerson | db.Persons.insert() |
| Delete Person by Id | /deletePerson/:id | db.Persons.remove() |
| Update single Person | /updatePerson | db.Persons.findAndModify() |
If you want to extend your knowledge beyond the list provided above, then I would suggest you to look into the very detailed and excellent documentation provided by MongoDB : http://docs.mongodb.org/manual/applications/crud/
Now lets look into the completed API of my nodejs, Here in line #4, AddressBook is mu collection name which is equivalent to the tables in RDBMS and Person is the document. If you are still confused with the document and collection and other jargons of MongoDB then probably you can look into the link: http://docs.mongodb.org/manual/reference/glossary/, this covers all the mogodb glossary.
I have also introduced body-parser here, which is used to parse the request and response in json format.
And again, for an extensive explanation of each of the operations I have used in this example below you can refer the MongoDB documentation: http://docs.mongodb.org/manual/applications/crud/
This is the least code you can write to implement the CRUD operation using mongodb and node, but the possibilities are limitless you you want to extend this example. Some of the things which you can do with very little effort if you want to make this solution more structure and object oriented are:
1. Use the application generator tool, express, to quickly create an application skeleton. http://expressjs.com/starter/generator.html
2. Use ORM/ODM tools like mongoose. Mongoose provides a straight-forward, schema-based solution to model your application data. It includes built-in type casting, validation, query building, business logic hooks and more, out of the box.
3. Use routing in Server side, though I have used routing in my client side but you can also use routing in middleware to keep more control of your code. You can learn more on these topics @ http://expressjs.com/starter/basic-routing.html OR http://expressjs.com/guide/routing.html
This is just few of options which can be done with very little effort but the possibilities are unlimited. You can fork or clone my example to extend it up to your capacity for learning purpose.
Now lets get back to the pending items which will wind up my post by creating Views
I am going to create a Dashborad which will display all the persons I have in my mongodb database in AddressBook collection. I have provided below the screenshot to show you how the completed code will look like. Here I have used AngularJS bindings for model binding and CSS3 and HTML5 to make my page look little descent.On click of Edit I am going to edit the person using a Model popup with pre-populated person data and similarly I am going to have a model popup for Add Person with empty controls which will allow the user to add a new person record and delete is simply going to remove the record from the mongodb. My example is designed to take you to the persondetail.html page which will have more details of the person, like person address but for the simplicity sake I have not implemented that in this example.
Now to achieve this I will need only few pages, which are listed below
index.html : This page is going to have only the references to the controller, angularjs, bootstrap and other client side libraries. This page also have a
This acts as a place holder for displaying the views injected by angularjs routing engine.<div data-ng-view="data-ng-view"></div>
person.html
This is the home page or default page in my application. Screenshots of this page is provided above. The purpose of this page is to list all the persons in the mongodb database, add, edit and delete the persons from mongodb.
I have used a very loosely defined model called person for my model and this is going to be saved in mongodb directly without any further transformation. My view is binded with persons model in the $scope through the PersonCtrl. Once the model is populated I have used ng-repeat to display all the persons in the table. In line #17, I have called the searchPerson with personId in PersonCtrl to get selected person to edit from mongodb. Here data-toggle and data-target is used to tell the Edit button to display the #personEditModal is clicked. And for delete in line #18 I have called deletePerson action in PersonCtrl, this in turn calls the NodeJs API for delete and remove the person from mongodb database. And for Add and Edit modal popup, I have a very basic design, refer the screenshot above. I have provided below the example of edit person, only difference of edit and add person is the display captions/headings and on click of save button, Edit calls the updatePerson(person) action in PersonCtrl and in add person modal this calls the addPerson(person) action in PersonCtrl.So with these code I have completed end to end data bindings, client side API using angular, server side API using nodejs and express and you might have noticed I have not done much like schema creation in mongodb data as mongodb is schema less document oriented database. So the same JSON which I have used to communicate between the different layers are stored directly into the mongodb database. But again this is just the start, the things I have not covered in mongodb is relation data example like person and person address documents and how they can be associated with each other using references, complex model bindings, etc. You can Clone, Form or download this example from my git repository @ https://github.com/bmdayal/MEANSample








While designing the webpage we often struggle to maintain the consistency of our site across different browsers. To achieve this we use different browser hacks. But again that ends up with making our programming more complicated.
In this post I am going to give you the link of a JavaScript using which you just need to know basics of CSS.
CSS Browser Selector is a very small JavaScript with just one line and less than 1kb which empower CSS selectors. It gives you the ability to write specific CSS code for each operating system and each browser.
This can work for the following Browsers
I think we have almost covered all the browsers right, if I am missing any let me know.
Instead of writing everything again here, and making this post lengthy I will suggest you to visit this site to download the JavaScript code and instructions on how to use this from here.
If you ever been working on layout and design of website then you may need to test the layout and rendering of the website in different browser, like IE 7, IE 6, IE 5.5, FireFox, Opera, etc.
But normally it is not possible to install multiple version IE in same system. So to get through this limitation of Windows, I found the following program using this you can run multiple version of IE (IE 7.0, 6.0, 5.5, 5.1, 4.0, 3.0) in the same System,
Normally this installer will not work with Windows Vista,
Follow the progress of running Internet Explorer 6 natively on Windows Vista here!You can get more details in Treadsoft Homepage
I hope this will help you.
CSS Expressions
CSS expressions were introduced in Internet Explorer 5.0 and it allows you to to assign a JavaScript expression to a CSS property. For example, this allows you to set the position of an element according to the browser size.
#myDiv {
position: absolute;
width: 100px;
height: 100px;
left: expression(document.body.offsetWidth - 110 + "px");
top: expression(document.body.offsetHeight - 110 + "px");
background: red;
}Not the best way to achieve this but it is easy enough to understand.
Dynamic Expressions
Not only does CSS expressions allow you to calculate the CSS value inside the style declarations, it also recalculates the expression so that property value is always correct. This means that the expression needs to be recalculated every time the document.body.offsetWidth is changed. If this was the case dynamic expressions would actually be a lot more useful. This isn't the case and recalculations of the expressions happens every time a JS execution thread is finished. This means that if you move your mouse and have a listener to onmousemove all the dynamic expressions are recalculated. This does not require you to be a genious to figure out that this can bring the simplest web application to a crawl.
Take a look at the following CSS block:
#myDiv {
border: 10px solid Red;
width: expression(ieBox ? "100px" : "80px");
}Asuming ieBox is a constant flag which is true when IE is in quirks mode, the expression will at all times result in "80px" (or "100px"). Even though the expression is constant it will be recalculated a lot of times. The question is how to remove these extra calculations for constant expressions.
Constant Expressions
The thing to do is to go through all the style sheet declarations and replace the constant expressions with a constant value. In the previous example, assuming we are using IE6 in standard mode, we want:
#myDiv {
border: 10px solid Red;
width: 80px;
}So, how do we find out if an expression is constant. The easiest way is to mark the expression so that it can be easily found. The solution we are going to use is to enclose the expression in a function that we know the name of and is equal to the identity function.
function constExpression(x) {
return x;
}And then in the CSS block we use:
#myDiv {
border: 10px solid Red;
width: expression(constExpression(ieBox ? "100px" : "80px"));
}Usage
The first thing you need to do is to include the cssexpr.js file. This should be done before any usage of constExpression.
<script type="text/javascript" src="cssexpr.js"></script>
After this you can use constExpression in any style block or any file included using a link element or using the @import directive in a these. Notice that style attributes are not checked due to performance reasons.
Implementation
The idea here is to go through all style sheets and then go through all rules and finally go through all the cssText. To do this we first go through the document.styleSheets collection.
function simplifyCSSExpression() {
try {
var ss,sl, rs, rl;
ss = document.styleSheets;
sl = ss.length
for (var i = 0; i < sl; i++) {
simplifyCSSBlock(ss[i]);
}
}
catch (exc) {
alert("Got an error while processing css. The page " +
"should still work but might be a bit slower");
throw exc;
}
}In the style sheet we go thorugh the imports collection and then the rules collection. We also check that the cssText contains "expression(constExpression(" so that we do not process the style sheet in vain.
function simplifyCSSBlock(ss) {
var rs, rl;
// Go through imports
for (var i = 0; i < ss.imports.length; i++)
simplifyCSSBlock(ss.imports[i]);
// if no constExpression we don't have to continue
if (ss.cssText.indexOf("expression(constExpression(") == -1)
return;
rs = ss.rules;
rl = rs.length;
for (var j = 0; j < rl; j++)
simplifyCSSRule(rs[j]);
}Now we go through the cssText of each rule and update the text, using a function named simplifyCSSRuleHelper, until the text does not change any more.
function simplifyCSSRule(r) {
var str = r.style.cssText;
var str2 = str;
var lastStr;
// update string until the updates does not change the string
do {
lastStr = str2;
str2 = simplifyCSSRuleHelper(lastStr);
} while (str2 != lastStr)
if (str2 != str)
r.style.cssText = str2;
}The helper function just finds the first expression and evaluates that and replaces the expression with the value.
function simplifyCSSRuleHelper(str) {
var i, i2;
i = str.indexOf("expression(constExpression(");
if (i == -1) return str;
i2 = str.indexOf("))", i);
var hd = str.substring(0, i);
var tl = str.substring(i2 + 2);
var exp = str.substring(i + 27, i2);
var val = eval(exp)
return hd + val + tl;
}Finally we need to call the function simplifyCSSExpression and this is done once the page is loaded.
if (/msie/i.test(navigator.userAgent) && window.attachEvent != null) {
window.attachEvent("onload", function () {
simplifyCSSExpression();
});
}Author: Erik Arvidsson