Node.js For .NET Developers

If you are a .NET developer and interested in learning Node.js, then the best tool is Visual studio 2015. You can download Visual studio 2015 community edition for free from the following download link.

Download Visual Studio

What is Node.js?

Node.js is a JavaScript framework created for server side scripting. So if we compare it with other programming languages  such as ASP.NET, the language is used for programming JavaScript for Node.js programming. Now we will learn some more details about Node.JS. First thing about Node.js is that it is open source.

Installation

The installation of Node.js  is simple and you can download the latest installer from Node.js site.

Installation

If you need to understand the code of Node.js, then it is also downloadable since Node.js is an open source programming language just like java.

After successfully installation you can see Node.js editor window.

NodeJS editor

Development environment for NodeJS

There are multiple development environments available for Node.js

Some are listed below:

  1. Nodeclipse.

    Nodeclipse

  2. Jetbrains.

    Jetbrains

  3. If you are not a .NET developer, then Visual studio is the best environment for Node.js development. First of all download Visual studio 2015 on your machine.

Advantages of NodeJS

  • Node.js is fast
  • Asynchronous programming
  • JavaScript is the programming language

What is npm?

Node.js package manager (npm) is a package manager for JavaScript, and is the default for Node.js. NPM is like nuget packages available in .NET framework. Using that we can easily add the latest packages in our application.

Start Node.JS development in visual studio

Open Visual Studio and create a new project

new project

javaScript

If you are unable to find any project after selection of JavaScript option, then you need to install JavaScript project templates in visual studio. Please find the url to download the template.

tools

After successful installation we are getting multiple Node.js project options

NodeJS projects

code

Part of Project
  1. Npm

    We can add npm packages in our project easily. So many npm packages are available online. After adding the npm packages we can add it in our application.

    adding the npm packages
    npm packages
  2. Declaration

    The syntax to add the npm packages into the code file:
    1. var express = require('express');  
    2. var routes = require('./routes');  
    3. var http = require('http');  
    4. var path = require('path');  
  3. Create a Server
    1. http.createServer(app).listen(app.get('port'), function () {  
    2.    console.log('Express server listening on port ' + app.get('port'));  
    3. });  
  4. Express

    Express is a Node.js web application framework. Please go through the following url to get more understanding on express modules like routes, exception handling and API.

  5. Asynchronous Programming or Callback

    It is very easy to develop Asynchronous programming in Node.js.
    1. var fs = require("fs");  
    2. var data = fs.readFileSync('input.txt');  

Async control flow patterns:

  1. Series - For running async tasks one at a time.
  2. Fully parallel - For running async tasks all at the same time.
  3. Limitedly parallel - For running a limited number of async tasks at the same time.

App.js

  1. /** 
  2.  * Module dependencies. 
  3.  */  
  4.   
  5. var express = require('express');  
  6. var routes = require('./routes');  
  7. var http = require('http');  
  8. var path = require('path');  
  9. var fs = require("fs");  
  10.   
  11. var app = express();  
  12.   
  13. // all environments  
  14. app.set('port', process.env.PORT || 3000);  
  15. app.set('views', path.join(__dirname, 'views'));  
  16. app.set('view engine''jade');  
  17. app.use(express.favicon());  
  18. app.use(express.logger('dev'));  
  19. app.use(express.json());  
  20. app.use(express.urlencoded());  
  21. app.use(express.methodOverride());  
  22. app.use(app.router);  
  23. app.use(require('stylus').middleware(path.join(__dirname, 'public')));  
  24. app.use(express.static(path.join(__dirname, 'public')));  
  25.   
  26. // development only  
  27. if ('development' == app.get('env')) {  
  28.     app.use(express.errorHandler());  
  29. }  
  30.   
  31. app.get('/', routes.index);  
  32. app.get('/about', routes.about);  
  33. app.get('/contact', routes.contact);  
  34.   
  35. http.createServer(app).listen(app.get('port'), function () {  
  36.       
  37.     var data = fs.readFileSync('input.txt');      
  38.     console.log(data.toString());  
  39.     console.log("Program Ended");  
  40.   
  41.     console.log('Express server listening on port ' + app.get('port'));  
  42. });  
View Template engine in Node.JS
 
Node.js is not only using HTML templates along with that it is also using jade template engine.
To get more understanding on this please visit the official website of jade template engine.

Please find the following sample code:
  1. extends layout  
  2.   
  3. block content  
  4.   .jumbotron  
  5.     h1 Express  
  6.     p.lead Express is a free web framework for building great Web sites and Web applications using HTML, CSS and JavaScript.  
  7.     p  
  8.       a.btn.btn-primary.btn-large(href='http://expressjs.com/') Learn more »  
  9.   .row  
  10.     .col-md-4  
  11.       h2 Getting started  
  12.       p Express gives you a powerful, patterns-based way to build dynamic websites that enables a clean separation of concerns and gives you full control over markup for enjoyable, agile development.  
  13.       p  
  14.         a.btn.btn-default(href='http://expressjs.com/') Learn more »  
  15.     .col-md-4  
  16.       h2 Get more libraries  
  17.       p Node Packaged Modules is a repository of software for Node.js.  
  18.       p  
  19.         a.btn.btn-default(href='https://www.npmjs.org/') Learn more »  
  20.     .col-md-4  
  21.       h2 Microsoft Azure  
  22.       p You can easily publish to Microsoft Azure using Node.js Tools for Visual Studio. Find out how you can host your application using a free trial today.  
  23.       p  
  24.         a.btn.btn-default(href='http://azure.microsoft.com') Learn more »  
One can easily convert html to jade and vice versa.