Thursday, June 25, 2015

AutoFill Attribute in Google Chrome

People consider about their valuable time a lot. When they fill a web form, if they get suggestion values, that would be a great advantage for them. Specially in mobile phones, if the web page suggests accurate values, that would be a supportive thing fr users.
Google chrome has provided autofill on forms using their browser.
As web developers only thing you have to do is use autocomplete attribute  correctly.
In WHATWG HTML Standard you can find more about name values for autocomplete attribute.

Monday, June 15, 2015

MicroServices?

What is MicroServices? Is it a re-branding of SOA or new architectural design pattern?
"MicroServices enable the agile development and delivery of complex enterprise applications. Light weight services with a narrow focus SOA with a new name."

Advantages

  • Small and easy to understand
  • Easy to scale
  • Easy to remove
  • Easy to deploy

Disadvantages

  • Need to consider about many separate services
  • Must automate for testing and deployment of each service
  • Difficult to manage, monitor larger number of separate services
  • Must consider about messaging patterns

Why NodeJS fit into Microservices Development?

  • Excellent package management
  • Narrow focus is a norm in node
  • Designed to end to end non blocking I/O
  • Excellent performance
  • Growth of javascsript stack

Sunday, June 14, 2015

Using Layouts With EJS in NodeJS

Layouts are essential thing to arrange your views in a well defined way. For EJS template engine, we can use express-ejs-layouts package. First install express-ejs-layouts as a npm package.
Then configure express-ejs-layouts in your express application like below. In here, I have added default layout file which is _main.ejs, located at ~views/layouts folder. And also there is another layout file called _login.ejs. But according to our configurations, _main.ejs file will be the default one.
In here you can see I have added ejs tag "<%-body%>" in the _main.ejs template. In the left side you can see signin.ejs which will use default layout.
If you want to use another layout except default layout, only thing you have to do is add that in controller action like below.
Download Source Code from GitHub.

Prevent CSRF Attacks in NodeJS Application

To prevent CSRF attacks we can use, csurf node protection middleware. First, you need to install it as a npm package using following command.
Then you need to enable csurf in your node application.
In a http form, you can add CSRF token as a hidden field.
Now you need to pass CSRF token from your node controller.
Run your application and you will see the CSRF token for above form.
Download Source Code from GitHub.

Debug NodeJS Application Using Node Inspector

Debugging a NodeJS application is a complicated task. But you can use Nodeinspector for make your life easier.
We will install Nodeinspector as a grunt task.
Then install grunt-concurrent. This module is used to run multiple Grunt tasks concurrently.
Now configure grunt tasks accordingly.
Then run grunt debug command. This will run your application in debug mode and start node-inspector server. Command line output should be like below.
You can open you web application and debug URL. Node-inspector will only work on browsers that use the Blink engine, such as Google Chrome or Opera. Therefore it's better to go with above browsers.
Download Source Code from GitHub.

Saturday, June 13, 2015

Test Automation NodeJS Application using Grunt

In my previous post I showed how to use Grunt in your NodeJS project. And also I have showed how to run NodeJS tests with mocha and how to run Jasmine tests with Karma. In this post I will show how to automate those tasks using Grunt.
I assume you have installed and configured Grunt, according to my previous post. Then install grunt-karma and grunt-mocha-test as npm packages.
Then configure grunt tasks for above modules.
To run test cases, only thing you have to do is run grunt test command in your command prompt against your project location.
Download Source Code from GitHub.

Using Grunt with NodeJS

You may have work with Ant or Rake to configure automation of repetitive tasks. For NodeJS, Grunt has become one of the easiest way to automate your repetitive tasks.
I have explained how to use Grunt with ASP.Net MVC project in my earlier post. In this post I will explain how to use Grunt with your NodeJS project.
First you need to install grunt-cli package globally.
And then install grunt as a npm package to your project.
As developers we may need to run our project in development mode or test mode. We can do that easily using grunt-env module. Install grunt-env as npm package.
In the GruntFile.js you can configure environment specific things easily.
When continuously developing your application, you will need to stop and start your application server frequently. To help with this task, there is a tool called Nodemon, which you can integrate to your NodeJS application using Grunt. First install nodemon as a npm package.
Then you need to configure nodemon grunt task.
In the options property, we configure Nodemon's operation and specify to watch both the HTML and JavaScript files that are placed in your config and app folders.
Runt grunt command in command prompt against to your project location.
Now do some changes in server.js file or any other *.js or *.html file which are in given locations. You do not have to restart your node server to check your changes. nodemon will automatically handle that for you. You will see a message for that in the command prompt.
Download Source Code from GitHub.

Node JS Application Skeleton Generators

If you have development experience with Node JS, you might have struggled a lot to create a better application skeleton for Node Express Application. And also, to identify required npm packages, you might have to browse through internet a lot. But express has provided express generator, which is an application scaffolding tool, to quickly create a application skeleton.
Assume you want to generate the application using EJS template engine. Only thing you have to do is run following command.
In target directory, following folders will be created
Run, npm install command to install required dependencies.
Furthermore, you could try tools like,

What is NodeJS Event Loop?

Javascript is single threaded language. NodeJS by extension is single threaded. This means Node application can do exactly one thing at a time. According to that explanation, you can't do parallel executions in node. But javascript have call back functions. This is handled by a concept called event loop by using an event queue.
Write following code in node and check the output.
Now, without waiting two seconds, set it as zero. You may expect result to be in an order of ABC. But check the output.
In javascript, one thread will start the execution of code. When you run that code, in the stack it will put pointer to function A. Then it will run function A. After finished function A, pointer for A in stack will be removed. Then it will add pointer B to stack. Within that function B, there's a time consuming code with a call back. Then javascript will handover to external process. Then callback pointer will be saved in the call back queue. Then Pointer B will be removed from the stack. Then pointer C will added to the stack and function C will execute. Then pointer C will be removed from the stack. Now there aren't anything in the stack. Then, event loop will check stack whether there are anything in the stack. If not, it will check call back queue. If there are, then those call back will push to stack. Then the callback code will run accordingly. Now you will be able to understand about above two programs.

Friday, June 12, 2015

Node.js Application Testing With Mocha

Testing is one of the most important thing in software development cycle. In a Node Express application we must properly test both controllers and models. In this post I will use Mocha as the test framework, should.js as assertion library and SuperTest for HTTP assertions. Mocha supports both TDD and BDD tests.
I will continue this post with the implementation which I have done in my previous post.
First we will install mocha globally.
Then install should.js and supertest as npm dev dependencies.
Now we need to configure our test environment. For that add test.js into config/env folder and add content like below.
In mocha test file, first you need to describe about your test. The describe blocks can be nested. Assume we need to write test for the following student.server.model.js model. Then you can create student.server.model.tests.js test file in app/tests folder like below.
In the test case, we will check two scenarios. One is, if we assign all values appropriately whether the student get saved. And the other one is, without passing required firstName whether the save function gives an error result. "beforeEach" and "afterEach" functions will run before and after each test is executed.
Now we will write code for test student.server.controller.js file. In here I will check "/api/students/" route functions.
Now it's time to run our mocha tests. In command prompt first set Environment as test.
Reason for that is, in server.js expects current environment.
Then run "mocha --reporter spec app/tests" command. The output will be look like below.
If you need to generate test results in a document, you could try "mocha-unfunk-reporter".
Download Source Code from GitHub.

Wednesday, June 3, 2015

Node JS User Authentication

In my last post, I explained about session management in NodeJS application.
Passport middleware helps to authenticate requests send to NodeJS application via Express. Passport middleware supports for both local and OAuth authentication providers, such as Facebook, Google and Twitter.
This post will continue with the implementations done in my last post. First you need to install passport and passport-facebook middlewares.
Then you need to change your User model.
Four new fields were added to UserSchema. salt field will contains a random string which will use to hash the user password. provider field will contains the authentication strategy used to register an user. providerId will contains the user identity for the authentication strategy. In providerData, you will save user object which you will retrieve from OAuth provider.
Next add following methods into user.server.model.js file.
Add signin.ejs and signup.ejs files.
Add SignIn and SignUp actions in user.server.controller.js file.
Configure routing for user in user.server.route.js file.
Now you have to configure passport-facebook middleware for the application. Add Facebook strategy configurations in to facebook.js file like below.
Add passport.js file like below.
Add passport instance for the application by modifying server.js file.
Configure express application for passport middleware.
For all above configurations you can read passport-facebook documentation. In the signin page , you will see "Sign in with Facebook" link. Using that you will be able to access the application and data will be saved into user table.