BLOGs
Design & Development
Published November 18th, 2016 by

Implementing Nodejs in Asp.Net MVC

nodejs

You will read about the concept of Node.js and its implementation in asp.net mvc in this article. Asp.net development company experts have intended this post for entire community of asp.net and developers that wish to work and implement NodeJs in mvc.

Web applications these days have changed drastically from the early stateless implementation of request response to asynchronous channel allowing two way communication between the client and the server. Along with this change, JavaScript has become omnipresent. It is used in mobile apps. Databases, web applications etc.

Node.js

The concept of Nodejs came in between these trends and allowed developers to create network applications using a core library that will make the best possible use of the I/O available.

Node is basically a platform created on JavaScript runtime for making scalable and fast network applications easily. It makes use of the event-driven model and non-blocking I/O.

web-server

The above diagram shows the working of a traditional server where a process is set for every incoming request. This process is expensive in terms of both memory use and CPU utilization. All incoming requests are managed by threads in this case. Each incoming request is assigned a thread by the server that is used for the entire lifetime of the request. This approach involves many resources and lots of processing.

Node.js uses an event loop that runs within a single thread. The JavaScript runtime contains a message queue that stores all messages that needs to be processed along with their callback functions. The messages represents the I/O calls having the registered callback. On completion of the I/O operation, the corresponding callback function is executed and the callback is inserted in event loop. Hence node can handle multiple operations in background apart from being as single threaded.

The below figure represents the event loop.

event-loop

As shown in the figure, there is a single thread to process all requests. Hence there is lesser memory use. Also, CPU utilization is less because of no context switching among threads. Node.js is very effective for applications that uses intensive data.

Node.js Modules

Node.js contains many built in modules providing a set of features for creating solutions based on node.js. These modules are loaded using the require() function. The identifiers of Node.js modules are passed to this function. Node contains three types of modules: file modules, external modules and core modules.

  1. File Modules:

Modules based on files have following specifications:

  1. Every file contains its own module.
  2. The module definition is accessed by the module variable for each file.
  3. The export of current module is determined by the module.export function.
  4. Modules can be imported into current file using require() function.
  1. Core Modules

The core modules can be consumed with the help of require function.  The main difference between the core modules and the file based modules is that in case of core modules, it is possible to pass the module name directly to the required function. Following are the core modules:

  1. Path: It helps in providing the string transformations with file systems.
  2. Fs: It provides access to file system. It contains functions for renaming, reading, writing and deleting files.
  3. OS: This module helps in providing access to some functionality of the operating system. eg. memory use, free memory etc.

In node.js, a module is loaded on the basis of its path. If its path is not specifies and the module is not the core module, then the Node will search the node modules folder.

External Modules:

These modules are available from third party sources and helps in providing rich functionality. These includes the modules for testing, document databases, relational databases etc. Programmers can build their own modules and upload the same on GitHub. Following is the utility used to load the modules:

npm install <modulename></modulename>

BUILDING AN MVC APPLICATION WITH NODEJS

  1. As a first step, download and install the NodeJs setup. Use the following to install it on Ubuntu.
sudo apt-get install nodejs
  1. Once nodejs is installed on the system, install the npm with the following command
sudo apt-get install nodejs npm

The above command will install all packages needed for nodejs.

  1. In the current example, we will use expressjs as the MVC framework because of its simplicity. Now make a directory for the website. Write the following command in its terminal:
sudo npm install express

Now it is required to specify the way of configuration for the application. Add a file named as index.js in the directory.

Create a definition for app as the web application of expressjs as shown below:

var express=require('express');
app=express()

Configure the application thereafter as shown below:

app.configure(function() {

app.set('view engine', 'jade');

app.set('views', __dirname + '/views');

app.use(express.logger());

app.use(express.bodyParser());

app.use(express.cookieParser());

app.use(express.static(__dirname + '/scripts'));

app.use(express.static(__dirname + '/css'));

app.use(express.static(__dirname + '/img'));

app.use(app.router);

});

Now add models and controllers to the application as shown below:

require('./models');
require('./controllers');

Create JavaScript objects for the models as per the requirement.  Finally add a listener to the init.js file.

Defining Controllers with Express

It is very easy to define controllers with express. Every action in a controller is a method that is defined by POST or GET, a function and the URL to call. A controller looks as follows:

app.get('/about', function(request, response) {

// just render the view called 'about'

// this requires us to have a file called 'about.jade' in our 'views' folder we defined

response.render('about');

});

app.get('/user/:userId', function(request, response) {

// userId is a parameter in the url request

response.writeHead(200); // return 200 HTTP OK status

response.end('You are looking for user ' + request.route.params['userId']);

});

app.post('/user/delete/:userId', function(request, response) {

// just a POST url sample

// going to this url in the browser won't return anything..

// do some work...

response.render('user-deleted'); // again, render our jade view file

});

You now know how to implement NodeJs in asp.net mvc. If there is any query related to implementation of NodeJs in asp.net mvc, ask asp.net development company experts straight in comments.

 

 

 

Johnny Morgan

Technical Writer at Aegis Infoways
Johnny Morgan as a technical writer at Aegis Infoways since more than 5 years. I write articles especially for Java, Python and Asp.Net. I have also got well response to write articles on CRM, Hadoop and QA.

Our rankings are completely independent, transparent, and community driven; they are based on user reviews and client sentiment. These design & development companies had to earn their way up and didn't just pay their way up.

View Rankings of Best Design & Development Companies