Node.js


Didn’t want the Node.js peeps to feel left out. So here it is.

Node.js is a free, open-sourced, cross-platform JavaScript run-time environment that lets developers write command line tools and server-side scripts outside of a browser.

nodejs

Node.js runs the V8 JavaScript engine, the core of Google Chrome, outside of the browser. This allows Node.js to be very performant.

A Node.js app is run in a single process, without creating a new thread for every request. Node.js provides a set of asynchronous I/O primitives in its standard library that prevent JavaScript code from blocking and generally, libraries in Node.js are written using non-blocking paradigms, making blocking behavior the exception rather than the norm.

When Node.js performs an I/O operation, like reading from the network, accessing a database or the filesystem, instead of blocking the thread and wasting CPU cycles waiting, Node.js will resume the operations when the response comes back.

This allows Node.js to handle thousands of concurrent connections with a single server without introducing the burden of managing thread concurrency, which could be a significant source of bugs.

Node.js has a unique advantage because millions of frontend developers that write JavaScript for the browser are now able to write the server-side code in addition to the client-side code without the need to learn a completely different language.

In Node.js the new ECMAScript standards can be used without problems, as you don’t have to wait for all your users to update their browsers – you are in charge of deciding which ECMAScript version to use by changing the Node.js version, and you can also enable specific experimental features by running Node.js with flags.

Enough with the Gyaan! Let’s see how to install Node.js on Windows 10!

The first steps in using Node.js is the installation of the Node.js libraries on the client system. To perform the installation of Node.js, perform the below steps;

Step 1) Go to the site https://nodejs.org/en/download/ and download the necessary binary files. In our example, we are going to the download the 64-bit setup files for Node.js.

Step 2) Double click on the downloaded .msi file to start the installation. Click the Run button on the first screen to begin the installation.

Step 3) On the next screen, click the “Next” button to continue with the installation

Step 4) On the next screen Accept the license agreement and click on the Next button.

Step 5) On the next screen, choose the location where Node.js needs to be installed and then click on the Next button.

1.First enter the file location for the installation of Node.js. This is where the files for Node.js will be stored after the installation.

2.Click on the Next button to proceed ahead with the installation.

Step 6) Accept the default components and click on the next button.

Step 7 On the next screen, click the Install button to start the installation.

Step 8) Click the Finish button to complete the installation.

Running your first Hello world application in Node.js

Once you have downloaded and installed Node.js on your computer, let’s try to display “Hello World” in a web browser.

Create file Node.js with file name helloworld.js

var http = require('http');

http.createServer(function (req, res) {
    res.writeHead(200, {'Content-Type': 'text/html'});
    res.end('Hello World!');
}).listen(1979);


Executing the code

  1. Save the file on your computer, something like: D:\Roopak\helloworld.js

Open Command Prompt, navigate to the folder where the file is stored. Enter the following command

Node helloworld.js

  1. Now, your computer has been upgraded to work as a web server 😉 If anyone tries to access your computer on port 1979, they will get a “Hello World!” message in return!
  2. Start your web browser, and type in the address: http://localhost:1979

In your Web Browser

It’s cool, Right? Enjoy. You are now a web-master.


Follow us on our Official Social Media Platforms.

What’s Deno?


So the apparent question is what the hell is Deno? Alright then, sit back and relax and let me explain it to you guys.

DENO is an anagram of NODE! Deno is a secure runtime for JavaScript and TypeScript that is based on the V8 JavaScript engine and the Rust programming language. It was created by Ryan Dahl, original creator of Node.js, and is focused on productivity. It was announced by Dahl in 2018 during his talk “10 Things I Regret About Node.js

Now, this doesn’t mean that Node.js is going to go away. No sir! It’ll still be used. However, Newer applications would most probably use Deno, since it doesn’t have the shortcomings of Node.js.

Deno uses V8 and is built in Rust.

  1. Secure by default. No file, network, or environment access, unless explicitly enabled.
  2. Supports TypeScript out of the box.
  3. Ships only a single executable file.
  4. Has built-in utilities like a dependency inspector (deno info) and a code formatter (deno fmt).
  5. Has a set of reviewed (audited) standard modules that are guaranteed to work with Deno

Installation

Deno ships as a single executable with no dependencies. You can install it using the installers below, or download a release binary from the releases page.

Shell (Mac, Linux):

$curl -fsSL https://deno.land/x/install/install.sh | sh

PowerShell (Windows):

$iwr https://deno.land/x/install/install.ps1 -useb | iex

Homebrew (Mac):

$brew install deno

Chocolatey (Windows):

$choco install deno

Scoop (Windows):

$scoop install deno

Build and install from source using Cargo

$cargo install deno

Getting Started

Try running a simple program:

$deno run https://deno.land/std/examples/welcome.ts

Or a more complex one:

import { serve } from "https://deno.land/std@0.57.0/http/server.ts";
const s = serve({ port: 8000 });
console.log("http://localhost:8000/");
for await (const req of s) {  req.respond({ body: "Hello World\n" });}

What DENO’s GOALS are?

  • Only ship a single executable (deno).
  • Provide Secure Defaults
    • Unless specifically allowed, scripts can’t access files, the environment, or the network.
  • Browser compatible: The subset of Deno programs which are written completely in JavaScript and do not use the global Deno namespace (or feature test for it), ought to also be able to be run in a modern web browser without change.
  • Provide built-in tooling like unit testing, code formatting, and linting to improve developer experience.
  • Does not leak V8 concepts into user land.
  • Be able to serve HTTP efficiently

Comparison to Node.js

  • Deno does not use npm
    • It uses modules referenced as URLs or file paths
  • Deno does not use package.json in its module resolution algorithm.
  • All async actions in Deno return a promise. Thus Deno provides different APIs than Node.
  • Deno requires explicit permissions for file, network, and environment access.
  • Deno always dies on uncaught errors.
  • Uses “ES Modules” and does not support require(). Third party modules are imported via URLs: import * as log from "https://deno.land/std/log/mod.ts";

Node.js developers and companies using it should not be worried by Deno. It’s not here to make you guys obsolete. It’s just another runtime environment, which is created by the same guy. Moreover, Deno is in a very nascent stage as of now. It’ll take time for it to mature and grow and be any threat to Node.js.

In the nutshell it looks like a very exciting runtime environment to me. I’m going to definitely give it a shot. How ’bout you?


Follow us on our Official Social Media Platforms.