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.
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.
Development environment for NodeJS
There are multiple development environments available for Node.js
Some are listed below:
- Nodeclipse.
- Jetbrains.
- 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
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.
After successful installation we are getting multiple Node.js project options
Part of Project - 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.
- Declaration
The syntax to add the npm packages into the code file:
- var express = require('express');
- var routes = require('./routes');
- var http = require('http');
- var path = require('path');
- Create a Server
- http.createServer(app).listen(app.get('port'), function () {
- console.log('Express server listening on port ' + app.get('port'));
- });
- 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.
- Asynchronous Programming or Callback
It is very easy to develop Asynchronous programming in Node.js.
- var fs = require("fs");
- var data = fs.readFileSync('input.txt');
Async control flow patterns:
- Series - For running async tasks one at a time.
- Fully parallel - For running async tasks all at the same time.
- Limitedly parallel - For running a limited number of async tasks at the same time.
App.js
-
-
-
-
- var express = require('express');
- var routes = require('./routes');
- var http = require('http');
- var path = require('path');
- var fs = require("fs");
-
- var app = express();
-
-
- app.set('port', process.env.PORT || 3000);
- app.set('views', path.join(__dirname, 'views'));
- app.set('view engine', 'jade');
- app.use(express.favicon());
- app.use(express.logger('dev'));
- app.use(express.json());
- app.use(express.urlencoded());
- app.use(express.methodOverride());
- app.use(app.router);
- app.use(require('stylus').middleware(path.join(__dirname, 'public')));
- app.use(express.static(path.join(__dirname, 'public')));
-
-
- if ('development' == app.get('env')) {
- app.use(express.errorHandler());
- }
-
- app.get('/', routes.index);
- app.get('/about', routes.about);
- app.get('/contact', routes.contact);
-
- http.createServer(app).listen(app.get('port'), function () {
-
- var data = fs.readFileSync('input.txt');
- console.log(data.toString());
- console.log("Program Ended");
-
- console.log('Express server listening on port ' + app.get('port'));
- });
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:
- extends layout
-
- block content
- .jumbotron
- h1 Express
- p.lead Express is a free web framework for building great Web sites and Web applications using HTML, CSS and JavaScript.
- p
- a.btn.btn-primary.btn-large(href='http://expressjs.com/') Learn more »
- .row
- .col-md-4
- h2 Getting started
- 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.
- p
- a.btn.btn-default(href='http://expressjs.com/') Learn more »
- .col-md-4
- h2 Get more libraries
- p Node Packaged Modules is a repository of software for Node.js.
- p
- a.btn.btn-default(href='https://www.npmjs.org/') Learn more »
- .col-md-4
- h2 Microsoft Azure
- 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.
- p
- a.btn.btn-default(href='http://azure.microsoft.com') Learn more »
One can easily convert html to jade and vice versa.