Docsity

INTRODUCTION TO NODE JS, Study Guides, Projects, Research of Web Programming and Technologies

Partial preview of the text

Download INTRODUCTION TO NODE JS and more Study Guides, Projects, Research Web Programming and Technologies in PDF only on Docsity!

CHAPTER

1:INTRODUCTION TO

NODEJS

Dr. Adeel Ansari

YOUR INSTRUCTOR

Dr. Adeel Ansari Assistant Professor/ Program Manager - PhD Computing/ Editor of JISR-C Journal Office: Room 102-B, 100 Building email: [email protected] Personal Portal: www.adeelansari.com My Biography: PhD in Information Technology, University Technology PETRONAS, Malaysia, 2016 MS (Software Engineering), PAF-KIET, Pakistan, 2011 MBA(MIS), PAF-KIET, Pakistan, 2008 BSc(Hons) in Computing, Staffordshire University, UK, 2006.

WHAT IS NODE.JS

Node.js is a server side platform built on Google Chrome’s JavaScript Engine (V8). Node.js was developed by Ryan Dahl in 2009 and is an open source, cross- platform runtime environment for developing server-side and networking applications. Node.js applications are written in JavaSCript, and can be run within the Node.js runtime on Windows and Linux. Node.js provides a rich library of various JavaScript modules which simplifies the development of web applications using Node.js to a great extent Node.js=Runtime Environment+JavaScript Library

FEATURES OF NODE.JS

Following are some of the important features that make Node.js the first choice of software architects.

1. Asynchronous and Event Driven – All APIs of Node.js library are asynchronous, that is, non-blocking. It essentially means a Node.js based server never waits for an API t o return data. The server moves to the next API after calling it and a notification of Events of Node.js helps the server to get a response from previous API call. 2. Very Fast – Being built on Google Chrome’s V8 JavaScript Engine, Node.js library is very fast in code execution. 3. Single Threaded but highly Scalable – Node.js uses a single threaded model with event looping. Event mechanism helps th server to respond in a non- blocking way and makes the server highly scalable as opposed to traditional servers which create limited threads to handle requests Node.js uses a single threaded program and the same program can provide service to a much larger

VAST NUMBER OF LIBRARIES

Npm with its simple structure helped the ecosystem of

Node.js proliferate, and now the npm registry hosts over

1,000,000 open source packages you can freely use.

AN EXAMPLE NODE.JS

APPLICATION

The most common example Hello World of Node.js is a web server: This code first includes the Node.js http module. Node.js has a fantastic standard library, including first-class support for networking. The createServer() method of http creates a new HTTP server and returns it. The server is set to listen on the specified port and hostname. When the server is ready, the callback function is called, in this case informing us that the server is running. Whenever a new request is received, the request event is called, providing two objects: a request (an http.IncomingMessage object) and a response (an http.ServerResponse const hostname = '127.0.0.1' const port = process.env.PORT const server = http.createServer((req, res) => { res.statusCode = 200 res.setHeader('Content-Type', 'text/plain') res.end('Hello World!\n') }) server.listen(port, hostname, () => { console.log(Server running at http://${hostname}:${port}/)

ANOTHER EXAMPLE OF

NODE.JS

var http = require('http'); http.createServer(function (req, res) { res.writeHead( 200 , {'Content-Type': 'text/plain'}); res.end('Hello World!'); }).listen( 8080 );

NODE.JS FRAMEWORKS AND

TOOLS

Node.js is a low-level platform. In order to make things easy and exciting for developers, thousands of libraries were built upon Node.js by the community. Many of those established over time as popular options. Here is a non-comprehensive list of the ones worth learning: AdonisJs: A full-stack framework highly focused on developer ergonomics, stability, and confidence. Adonis is one of the fastest Node.js web frameworks. Express: It provides one of the most simple yet powerful ways to create a web server. Its minimalist approach, unopinionated, focused on the core features of a server, is key to its success.

NODE.JS FRAMEWORKS AND

TOOLS

Meteor: An incredibly powerful full-stack framework, powering you with an isomorphic approach to building apps with JavaScript, sharing code on the client and the server. Once an off-the-shelf tool that provided everything, now integrates with frontend libs React, Vue, and Angular. Can be used to create mobile apps as well. Micro: It provides a very lightweight server to create asynchronous HTTP microservices. NestJS: A TypeScript based progressive Node.js framework for building enterprise- grade efficient, reliable and scalable server-side applications. Next.js: A framework to render server-side rendered React applications. Nx: It powers the Angular CLI which allows building full-stack applications using NestJS, Express, and Angular and easily share code between backends and frontends. Socket.io: A real-time communication engine to build network applications.

WHO USES NODE.JS

There is an exhaustive list of projects, application and

companies which are using Node.js. This list includes eBay,

General Electric, GoDaddy, Microsoft, PayPal, Uber, Wikipins,

Yahoo! And Yammer to name a few.

WHERE NOT TO USE

NODE.JS

It is not advisable to use Node.js for CPU intensive applications.

WHAT CAN NODE.JS DO?

Node.js can generate dynamic page content Node.js can create, open, read, write, delete, and close files on the server Node.js can collect form data Node.js can add, delete, modify data in your database

CONCEPTS

The following diagram depicts some important parts of Node.js which we will discuss in detail in the subsequent chapters.

NODE.JS

BUILT-IN

MODULES

Node.js has a set of built-in modules which you can use without any further installation. Here is a list of the built-in modules of Node.js version 6.10.3: Module Description assert Provides a set of assertion tests buffer To handle binary data child_process To run a child process cluster To split a single Node process into multiple processes crypto To handle OpenSSL cryptographic functions dgram Provides implementation of UDP datagram sockets dns To do DNS lookups and name resolution functions domain Deprecated. To handle unhandled errors events To handle events fs To handle the file system http To make Node.js act as an HTTP server https To make Node.js act as an HTTPS server. net To create servers and clients os Provides information about the operation system path To handle file paths punycode Deprecated. A character encoding scheme querystring To handle URL query strings readline To handle readable streams one line at the time stream To handle streaming data string_decoder To decode buffer objects into strings timers To execute a function after a given number of milliseconds tls To implement TLS and SSL protocols tty Provides classes used by a text terminal url To parse URL strings util To access utility functions v8 To access information about V8 (the JavaScript engine) vm To compile JavaScript code in a virtual machine zlib To compress or decompress files