Professional Documents
Culture Documents
Review Webserversss
Review Webserversss
Review Webserversss
JS
What is Node.js
It is a runtime environment for JavaScript that runs on the server. It is open source,
cross-platform, and since its introduction in 2009, it got hugely popular and now
plays a significant role in the web development scene.
It runs the V8 JavaScript engine, the core of Google Chrome, outside of the browser.
Node.js is able to leverage the work of the engineers that made (and will continue to
make) the Chrome JavaScript runtime blazing fast, and this allows Node.js to benefit
from the huge performance improvements and the Just-In-Time compilation that V8
performs.
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 with flags.
History of Node.js
JavaScript is a programming language that was created at Netscape as a
scripting tool to manipulate web pages inside their browser, Netscape
Navigator.
Part of the business model of Netscape was to sell Web Servers, which
included an environment called Netscape LiveWire that could create
dynamic pages using server-side JavaScript. Unfortunately, Netscape
LiveWire wasn't very successful and server-side JavaScript wasn't
popularized until recently, by the introduction of Node.js.
One key factor that led to the rise of Node.js was the timing. Just a few
years earlier, JavaScript had started to be considered as a more serious
language, thanks to "Web 2.0" applications (such as Flickr, Gmail, etc.) that
showed the world what a modern experience on the web could be like.
JavaScript engines also became considerably better as many browsers
competed to offer users the best performance. Development teams behind
major browsers worked hard to offer better support for JavaScript and find
ways to make JavaScript run faster. The engine that Node.js uses under the
hood, V8 (also known as Chrome V8 for being the open-source JavaScript
engine of The Chromium Project), improved significantly due to this
competition.
Node.js was written and introduced by Ryan Dahl in 2009
Year Improvements
Node.js is born
2009 The first form of npm is created
Express is born
2010 Socket.io is born
The leftpad incident
Yarn is born
2016
Node.js 6
Node.js 10
2018 ES modules .mjs experimental support
Application of Node.js
Real Time Chats,
Complex Single-Page applications,
Real-time collaboration tools,
Streaming apps
To verify the installation, open the command prompt and type node –v. If
Node.js is installed successfully then it will display the version of the
Node.js installed on your machine as shown on the right.
To exit from the REPL terminal, press Ctrl + C twice or write .exit and press
Enter.
Thus, you can execute any Node.js/JavaScript code in the node shell
(REPL). This will give you a result which is similar to the one you will get in
the console of Google Chrome browser.
Functions
o Functions are first class citizens in Node's JavaScript, similar to
the browser's JavaScript. A function can have attributes and
properties also. It can be treated like a class in JavaScript.
Defaults to Local
o Node's JavaScript is different from browser's JavaScript when it
comes to global scope. In the browser's JavaScript, variables
declared without var keyword become global. In Node.js,
everything becomes local by default.
Now, you can import log object using require() function and use it anywhere in your
Node.js project.
Node Modules
o 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.
o Each module in Node.js has its own context, so it cannot
interfere with other modules or pollute global scope. Also, each
module can be placed in a separate .js file under a separate
folder.
o js implements CommonJS modules standard. CommonJS is a
group of volunteers who define JavaScript standards for web
server, desktop, and console application.
o js Module Types:
js Core Modules - Node.js is a light weight 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.
o 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. createServer().
o In this way, you can load and use Node.js core modules in your
application.
o
A local module can be created using module.exports,
which can be used in an application.
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.
For example, if you need to connect to MongoDB and
fetch data then you can create a module for it, which
can be reused in your application.
Export Modules in Node.js
The module.exports or exports is a special object which is included in every JS file in
the Node.js application by default. module is a variable that represents current
module and exports is an object that will be exposed as a module. So, whatever you
assign to module.exports or exports, will be exposed as a module. Let's see how to
expose different types as a module using module.exports.
Export Literals
o As mentioned above, exports is an object. So it exposes
whatever you assigned to it as a module. For example, if you
assign a string literal then it will expose that string literal as a
module.
o The following example exposes simple string message as a
module in Message.js.
o Now, import this message module and use it as shown below.
o Run the above example and see the result as shown below.
o Note: You must specify './' as a path of root folder to import a local
module. However, you do not need to specify path to import Node.js
core module or NPM module in the require() function.
Export Object
exports is an object. So, you can attach properties or methods to it. The following
example exposes an object with a string property in Message.js file.
Run the above example and see the result as shown on the next slide.
Export Functions
You can attach an anonymous function to exports object as shown below.
In this way, you can export and import a local module created in a separate file under
root folder.
Node.js also allows you to create modules in sub folders. Let's see how to load
module from sub folders.
In the example below, Node will search for a package definition file called
package.json inside utility folder. This is because Node assumes that this folder is a
package and will try to look for a package definition. The package.json file should be
in a module directory. The package.json under utility folder specifies the file name
using "main" key as below.
Now, Node.js will find log.js file using main entry in package.json and import it.
NPM performs the operation in two modes: global and local. In the global
mode, NPM performs operations which affect all the Node.js applications
on the computer whereas in the local mode, NPM performs operations for
the particular local directory which affects an application in that directory
only.
Install Package Locally
o Use the following command to install any third party module in
your local Node.js project folder.
o For example, the following command will install ExpressJS into
MyNodeProj folder.
o All the modules installed using NPM are installed
under node_modules The above command will create ExpressJS
folder under node_modules folder in the root folder of your
project and install Express.js there.
o For example, the following command will install ExpressJS in
your application and also adds dependency entry into the
package.json.
o NPM can also install packages globally so that all the node.js
application on that computer can import and use the installed
packages. NPM installs global packages
into /<User>/local/lib/node_modules
o Apply -g in the install command to install package globally. For
example, the following command will install ExpressJS globally.
Update Package
o To update the package installed locally in your Node.js project,
navigate the command prompt or terminal window path to the
project folder and write the following update command.
o The following command will update the existing ExpressJS
module to the latest version.
Uninstall Packages
o Use the following command to remove a local package from
your project.
o The following command will uninstall ExpressJS from the
application.
This is how you create a Node.js web server using simple steps. Now, let's see how to
handle HTTP request and send response in Node.js web server
In the provided code above, url is used to check the url of the current request and
based on that it sends the response. To send a response, first it sets the response
header using writeHead() method and then writes a string as a response body
using write() method. Finally, Node.js web server sends the response
using end() method
Now, run the above web server as shown below.
To test it, you can use the command-line program curl, which most Mac and Linux
machines have pre-installed.
The same way, point your browser to http://localhost:5000/studentand see the
following result.
It will display "Invalid Request" for all requests other than the above URLs.
The following example demonstrates how to serve JSON response from the Node.js
web server.