Before starting to explain how you start your first Node.js project for building a simple web server I will first explain you what Node.js is. To do so I just include a quote of the Node.js themself, because I don’t like to reinvent the wheel.
The next thing you should know is about the Node.js Package Manager (NPM). Using NPM you can benefit from the thousands of open source packages so you won’t have to build everything yourself.
I suppose you already have installed Node.js. Let’s start with a simple Hello World in Node.js by creating a file called hello-world.js and execute our script to show the result.
We can execute the script using following command. (On Windows open the Node.js command prompt) Navigate to your folder containing the just created hello-world.js file and execute the script using node.
Your environment has been set up for using Node.js 0.10.17 (x64) and npm.
Press any key to continue . . .
C:\Users\Marco> cd MyHelloWorldCodeFolder
C:\Users\Marco\MyHelloWorldCodeFolder> node hello-world.js
The year 2012 was for me a year that went way to fast. In the year 2012 I learned a lot new stuff, wrote several blog posts and read lots of blog posts and articles. First of all I want you to give a list of all blog posts I wrote this year. You can find the complete list here http://marcofranssen.nl/2012/ and here http://marcofranssen.nl/2012/page/2/.
Second I want to share my starred articles from my reading archive. I used pocket to bookmark, read and archive my articles, which enables me now to share all starred articles with you. Continue reading →
Why is it bad to have all your script code available at global level?
First of all you can possibly get a lot of errors when using modules developed by others, because you used the same names for you variables etc.
The second reason is, this code can’t be minified as good as the code I will show you later.
In my example I will use a pattern like in the first article because it will allow me to control visibility.
This way of writing your code looks pretty much like writing your code in c# or Java, isn’t it (except from the syntax).
But we are still polluting the global namespace. Now there are only two variables, but think of it when you complete the code I started. How many modules would be added? How much more you will be polluting the global namespace?
When opening the developer tools in for example Chrome and hitting F12 and navigating to the console tab and you type ‘product’ or ‘shoppingCart’ and hit the enter key you can access the objects from the global namespace.
So how can we wrap these modules in our own namespace?
As you can see we build a little wrapper around our modules. This wrapper is a self executing function which provides access to the elements from the global namespace. When calling this anonymous self executing function we provide our own namespace which we register at global level. We also provide window, document and undefined, because this gives advantages in performance and minification of your scripts. When you never use them in your script you don’t need to add them, but as a best practice I always add them so I will never forget them. Please note that the last parameter isn’t provided, so it is undefined.
Another best practice is to start each script with a semicolon, so you don’t have to bother about missing semicolons in other scripts. By starting with one at least this script file will not give errors on behalf of missing semicolons in other files. Issues most of the time occur when minifying your scripts.
Last but not least we have to register our module in our namespace. What we do is checking if the module already exists or else replace it with your module definition.
You may be asking, what if I want to use jQuery or whatever other script? Just add it to the wrapper so you can use it in you module.
In a previous post I showed you some pseudo code Gregory Young mentioned in his DDD CQRS course I attended in Krakow, Poland. In this course Greg made clear to us locking of databases isn’t necessary. He showed us some pseudo code how to easily write a merge handler to handle all concurrency conflicts.
In my current project, based on the ncqrs-framework I implemented a simpler version of this merge handler which only retries each command if a ConcurrencyException occurs. To achieve this you can easily build a wrapper around the ncqrs commandservice.
I’m still searching for a better name for my class, because SafeCommandExecutor isn’t the best name I think. So let me know if you have a better one. I hope this article will be useful for you guys. As always, please share…
If you improved my code let me know, so I can learn from it.
Second I need to init my products overview.
In order to let them communicate with each other on a specific page I implement some mediator, who will link some logic together for a specific page. At first glance this looks like overkill, and I see you thinking why not ding this in the page itself. Well when your modules have more events and more logic to link to each other this can grow into larger code. When the amount of modules on a page grows, you want a central place where you link all logic together. You can implement more mediators in case this keeps your code cleaner.
Last but not least you have to add these scripts to your page.
As always, share this post if you liked it! If you didn’t also share it .
Declarative bindings: (Easily associate DOM elements with model data using a concise, readable syntax);
Automatic UI Refresh: (When your data model’s state changes, your UI updates automatically)
Dependency tracking: (Implicitly set up chains of relationships between model data, to transform and combine it)
Templating: (Quickly generate sophisticated, nested UIs as a function of your model data)
In the example below I use jQuery to get some json server data.
The Json server data should be an array of objects containing a ‘value’ and ‘text’ property. As you can see I use the ‘html5′ ‘data-bind attribute’ to map my view model to my UI elements.
For example your ASP.NET MVC3 action could look like this.
I wrote the examples in notepad, so there could be some issues. However if I did my job well this should be all to let all the magic happen.