Thursday, April 5, 2018

TypeScript



TypeScript is an open-source programming language developed and maintained by Microsoft. 


Every JavaScript program is also a TypeScript program. It is a strict syntactical superset of JavaScript with design-time support for type safety and tooling. TypeScript adds optional types, classes, and modules to JavaScript.

TypeScript is designed for development of large applications and transpile to JavaScript

TypeScript is a primary language for Angular application development. 

Browsers can't execute TypeScript directly. Typescript must be "transpiled" into JavaScript using the tsc compiler, which requires some configuration. The TypeScript compiler is itself written in TypeScript and compiled to JavaScript.

The TypeScript compiler performs only file local transformations on TypeScript programs and does not re-order variables declared in TypeScript. This leads to JavaScript output that closely matches the TypeScript input. TypeScript does not transform variable names, making tractable the direct debugging of emitted JavaScript. TypeScript optionally provides source maps, enabling source-level debugging. TypeScript tools typically emit JavaScript upon file save, preserving the test, edit, refresh cycle commonly used in JavaScript development.

TypeScript compiles to clean, simple JavaScript code which runs on any browser, in Node.js, or in any JavaScript engine that supports ECMAScript 3 (or newer).

The typescript was first made public in October 2012 (at version 0.8).On 22 September 2016, TypeScript 2.0 was released; it introduced several features, including the ability for programmers to optionally prevent variables from being assigned null values.

Installing
For the latest stable version: 
npm install -g  typescript

Building your first TypeScript file

In your editor, type the following JavaScript code in greeter.ts:
function greeter(person) {
    return "Hello, " + person;
}

let user = "Jane User";

document.body.innerHTML = greeter(user);

Compiling your code

We used a .ts extension, but this code is just JavaScript. You could have copy/pasted this straight out of an existing JavaScript app.
At the command line, run the TypeScript compiler:
tsc greeter.ts
The result will be a file greeter.js which contains the same JavaScript that you fed in. We’re up and running using TypeScript in our JavaScript app!

Running your TypeScript web app

Now type the following in greeter.html:
<!DOCTYPE html>
<html>
    <head><title>TypeScript Greeter</title></head>
    <body>
        <script src="greeter.js"></script>
    </body>
</html>
Open greeter.html in the browser to run your first simple TypeScript web application!

For more docs about typescript: www.typescriptlang.org/docs/handbook/typescript-in-5-minutes

Wednesday, January 17, 2018

Node.js Modules

for Basic About Node JS: http://minervajeyarajah.blogspot.com/2017/05/node-js.html



A module in Node.js is a simple or complex functionality organized in single or multiple JavaScript files which can be reused throughout the Node.js application.



Consider modules to be the same as JavaScript libraries, a set of functions you want
to include in your application.


Module Types:

Node.js includes three types of modules:
  1. Core Modules
  2. Local Modules
  3. Third Party Modules

Node.js Core Modules:

Node.js is a lightweight framework.
The core modules include bare minimum functionalities of Node.js.
These core modules are compiled into its binary distribution and load automatically
when Node.js process starts.
However, you need to import the core module first in order to use it in your application.
The following table lists some of the important core modules in Node.js.
Core Module
Description
http module includes classes, methods and events to create Node.js http server.
url module includes methods for URL resolution and parsing.
fs module includes classes, methods, and events to work with file I/O.
path module includes methods to deal with file paths.


Loading Core Modules:

In order to use Node.js core or NPM modules, you first need to import it
using require() function as shown below.
var module = require('module_name');
As per above syntax, specify the module name in the require() function.
The require() function will return an object, function, property or any other JavaScript type, depending on what the specified module returns.
var http = require('http');
var server = http.createServer(function(req, res){
//write code here
});
server.listen(8080);


In the above example, require() function
returns an object because http module returns
its functionality as an object, you can then use its properties and methods using dot notation
e.g. http.createServer().
In this way, you can load and use Node.js core modules in your application.

Node.js Local Module:

Local modules are modules created locally in your Node.js application.
These modules include different functionalities of your application in separate files and folders.
You can also package it and distribute it via NPM, so that Node.js community can use it.

Writing Simple Module:

Let's write simple logging module which logs the information, warning or error to the console.
In Node.js, module should be placed in a separate JavaScript file. So, create a log.js file and write the following code in it.
log.js
var log = {
info: function (info) { console.log('Info: ' + info);
},
warning:function (warning) { console.log('Warning: ' + warning);
},
error:function (error) { console.log('Error: ' + error); }
}; module.exports = log
In the above example of logging module, we have created an object with three functions - info(), warning() and error().
At the end, we have assigned this object to module.exports.
The module.exports in the above example exposes a log object as a module.
The module.exports is a special object which is included in every JS file in the Node.js application by default.
Use module.exports or exports to expose a function, object or variable as a module in Node.js.
Now, let's see how to use the above logging module in our application.

Loading Local Module:

To use local modules in your application, you need to load it using require() function in the same way as core module.
However, you need to specify the path of JavaScript file of the module.
The following example demonstrates how to use the above logging module contained in Log.js.
app.js
var myLogModule = require('./Log.js');
myLogModule.info('Node.js started');
In the above example, app.js is using log module.
First, it loads the logging module using require() function and specified path where logging module is stored.
Logging module is contained in log.js file in the root folder.
So, we have specified the path './log.js' in the require() function.
The '.' denotes a root folder.
The require() function returns a log object because logging module exposes an object in log.js using module.exports.
So now you can use logging module as an object and call any of its function using dot notation
e.g myLogModule.info() or myLogModule.warning() or myLogModule.error()
Now you can run the application and can see the status in your terminal
Thus, you can create a local module using module.exports and use it in your application.
For more on Node Module : https://nodejs.org/api/modules.html


My First Day with Uki DN Cohort 1

 "Hello WORLD!"