In this post we will be discussing about getting started with Node.js having C# or more specifically .Net development background. C# is a simple, modern, general-purpose, object-oriented programming language developed by Microsoft whereas Node.js is a JavaScript runtime built on Chrome's V8 JavaScript engine. Node.js uses an event-driven, non-blocking I/O model that makes it lightweight and efficient.
The first thing when starting with the Node.js is to know the processing model of Node.js because it totally differs from that of .Net (Asp.Net). Node.js has asynchronous processing style whereas .Net offers programmer a choice to go with Asynchronous or Synchronous (like async).
As you can see in the Node.js processing model Node.js uses one thread for handling requests and many thread to provide asynchronous non-blocking IO. Whereas in Asp.Net, it uses restricted number of concurrent requests pool of threads and queue requests to it. In Asp.Net there can be a context switching between the threads thus adding CPU costs.
ALSO READ : Getting Started with Node.js
C# Reversing a String using Inline Functions
Func<string, string> rev = (string input) =>
{
var arr = input.ToCharArray();
Array.Reverse(arr);
return sting.Join("", arr);
};
rev("Hello");
Node.js revering a String
var reverse = function(imput) {
return input.split("").reverse().join("");
};
reverse("Hello");
C# callbacks using delegates
public class Program
{
delegate void CallBackDelegate(sting input);
public static void Main()
{
CallBackDelegate cd = new CallBackDelegate(CallbackFunc);
cd("Hello");
}
static void CallbackFunc(string input)
{
Console.WriteLine(input);
}
}
Node.js Callbacks
var callback = function(str) {
console.log(str);
};
callback("hello");
ALSO READ : Write .Net and Node.js code together using Edge.js
Hello World in Asp.Net Katana
using owin;
namespace HelloWorld
{
public class Startup
{
public void Configuration(IAppBuilder app)
{
app.Run(async (context) => {
await context.Response.WriteAsync("Hello World!");
});
}
}
}
Hello World in Node.js
var http = require('http');
var requestListener = function (request, response) {
response.writeHead(200, {'Content-Type': 'text/plain'});
response.end('Hello World\n');
}
var server = http.createServer(requestListener);
server.listen(8080); // The port where you want to start with.
Node processes your request but it doesn't wait for the back-end process to complete before moving onto the next request. So when one request is processed Node attaches a callback function to the request, so whenever the request is ready to be responded, an event is called which trigger the associated callback function to send the response.
Please Like and Share CodingDefined.com blog, if you find it interesting and helpful.
The first thing when starting with the Node.js is to know the processing model of Node.js because it totally differs from that of .Net (Asp.Net). Node.js has asynchronous processing style whereas .Net offers programmer a choice to go with Asynchronous or Synchronous (like async).
Picture Demonstration of Node.js Server
Picture Demonstration of ASP.NET Server
As you can see in the Node.js processing model Node.js uses one thread for handling requests and many thread to provide asynchronous non-blocking IO. Whereas in Asp.Net, it uses restricted number of concurrent requests pool of threads and queue requests to it. In Asp.Net there can be a context switching between the threads thus adding CPU costs.
ALSO READ : Getting Started with Node.js
Examples in C# and how it can be implemented in Node.js
C# Reversing a String using Inline Functions
Func<string, string> rev = (string input) =>
{
var arr = input.ToCharArray();
Array.Reverse(arr);
return sting.Join("", arr);
};
rev("Hello");
Node.js revering a String
var reverse = function(imput) {
return input.split("").reverse().join("");
};
reverse("Hello");
C# callbacks using delegates
public class Program
{
delegate void CallBackDelegate(sting input);
public static void Main()
{
CallBackDelegate cd = new CallBackDelegate(CallbackFunc);
cd("Hello");
}
static void CallbackFunc(string input)
{
Console.WriteLine(input);
}
}
Node.js Callbacks
var callback = function(str) {
console.log(str);
};
callback("hello");
ALSO READ : Write .Net and Node.js code together using Edge.js
Hello World in Asp.Net Katana
using owin;
namespace HelloWorld
{
public class Startup
{
public void Configuration(IAppBuilder app)
{
app.Run(async (context) => {
await context.Response.WriteAsync("Hello World!");
});
}
}
}
Hello World in Node.js
var http = require('http');
var requestListener = function (request, response) {
response.writeHead(200, {'Content-Type': 'text/plain'});
response.end('Hello World\n');
}
var server = http.createServer(requestListener);
server.listen(8080); // The port where you want to start with.
Node processes your request but it doesn't wait for the back-end process to complete before moving onto the next request. So when one request is processed Node attaches a callback function to the request, so whenever the request is ready to be responded, an event is called which trigger the associated callback function to send the response.
Please Like and Share CodingDefined.com blog, if you find it interesting and helpful.
Related articles
- Getting Started with Express - Installation and Setup
- Node.js CMS Frameworks
- Capture Screen of Web Pages through URL in Nodejs
- Web Scraping in Node.js
- Getting familiar with Grunt JS - Part 1
- Vorlon.js - Remotely Test and Debug JavaScript Code
- Exploring JSDOM in Node.js
- NodeJS development in Visual Studio
- Containerized Unit Testing Nodejs Applications with Dockunit
- How to solve npm Error : ENOENT, stat in Nodejs
Except that no one with more than 0.5 grams of encephalitic mass who has ever used any properly typed language would bother dealing with retarded useless untyped javascript on the server. Thanks.
ReplyDelete