Node Js
Node Js
js
What is Node.js?
Node.js is an open-source server environment
Node.js is free
Node.js runs on various platforms (Windows, Linux, Unix, Mac OS X, etc.)
Node.js uses JavaScript on the server
Primitive Types
Node.js includes following primitive types:
String
Number
Boolean
Undefined
Null
RegExp
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.
1. Core Modules
2. Local Modules
3. Third Party Modules
Core Modules
In Node.js, core modules are built-in modules that are included in the Node.js distribution. These
modules provide basic functionality that is essential for building Node.js applications.
Core modules can be loaded using the require() function without the need for any additional
installation or configuration.
Syntax:
const fs=require(‘fs’);
console.log(‘__dirname’);
2. External Modules:
External modules are third-party modules that are not included in the Node.js core
distribution, and you need to install them separately using the Node Package Manager
(npm). Some popular external modules include:
Global Modules:
Global modules are modules that are installed globally on your system using the Node Package
Manager (npm). These modules can be used in any project or application on your system,
regardless of where they are stored. Like console.log().
We don’t need to use any other function to use that module and we can access it by simply because it is
a global module.
Local Modules:
Local modules are modules that are specific to a particular project or application. These modules are
created by the developer and are stored within the project directory.
To use a local module in Node.js, you need to provide the file path of the module using require()
function. For example, if you have a module named "example.js" located in the same directory as
your main Node.js file, you can load it using the following code:
Example:
let example = require('./example');
Local modules are specific to a project and are installed locally, while global modules can be
used in any project and are installed globally.
In Node.js, http is a core module that provides functionality to create HTTP servers and clients.
Using the http module, you can create a web server that listens for incoming HTTP requests and
returns responses to those requests. You can also create an HTTP client that sends HTTP
requests to a server and receives responses.
Const http=require(‘http’)
Const server=http.createServer((req,res)=> {
If(req.url===’/’){
res.write(‘Hello world’);
res.end();
}
});
server.listen(4000);
console.log(“How are you Bilal’);
Package.json
In Node.js, package.json is a file that is used to describe a Node.js package/module. It contains
important information about the package, such as its name, version, dependencies, and other
metadata.
Here's an overview of some of the key information that can be found in a package.json file:
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.
The following example demonstrates how to use Node.js http module to create a web server.
});
server.listen(5000);
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 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
let 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.
The following example demonstrates how to use the above logging module contained in Log.js.
app.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()
Run the above example using command prompt (in Windows) as shown below.
Output
You can save anything in the export module by using module.exports function.
When you have to call that module { module.exports}, you can call it by require.
Example:
App.js
Index.js
console.log(msg);
Output:
Hello world
Note:
You must specify ./ as a path of root folder to import a local module. However, you do not need
to specify the path to import Node.js core modules or NPM modules in the require() function.
Export Object
The exports is an object. So, you can attach properties or methods to it. The following
example exposes an object with a string property in app.js file.
Example
app.js
//or
Index.js
console.log(msg.SimpleMessage);
Output
Hello World