Getting Started With Node JS Modules

Spread the love

Modules are one of the fundamental features of Node.js. When you are building an application, as the code gets more and more complex you cannot put the entire code in a single file.

As this gets complex and unmanageable, you can use Node JS modules to write different files and export them (including functions, methods, and objects) to the main file.

Now you might wonder what exactly is a module?

In simple terms, a module is nothing but a JavaScript file.

With Node’s modular functionality, you can import your own external files, core node modules, and NPM modules.

How to create Node JS Modules and each of its components?

1. Require
2. Exports
3. Module
4. Imports

Require

require are used to consume modules. It allows you to include modules in your programs. You can add built-in core Node.js modules, community-based modules, and local modules.

The require function will look for files in the following order:

1. Built-in
2. NPM Modules
3. Local Modules

Built-In Modules

Node.js has a set of built-in modules which you can use without any further installation. Some of the most used core modules are:

1. fs
2. path
3. http
4. url

NPM Modules

NPM modules are third-party modules that you can use only after installing them. To name a few:

1. Lodash
2. Request
3. Express

You have to install them first, like – npm install express

If you could not find a third-party library, you will have to develop it yourself. And you can do this using exports.

Exports

The exports keyword gives you the chance to “export” your objects and methods.

Imports

Starting with version 8.5.0+, Node.js supports ES modules natively with a feature flag and new file extension *.mjs.

The Built-In HTTP Module

Node.js has a built-in module called HTTP, which allows Node.js to transfer data over the Hyper Text Transfer Protocol (HTTP).

To include the HTTP module, use the require () function with the name of the module:

built-in-http-module

Now your application has access to the HTTP module, and is able to create a server:

built-in-http-module-002

Node.js as a Web Server

The HTTP module can create an HTTP server that listens to server ports and gives a response back to the client.

Use the createServer() method to create an HTTP server:

Example:

nodejs-as-webserver.

Run Example>>

The function passed into the http.createServer() method, will be executed when someone tries to access the computer on port 8080.

Save the code above in a file called “demo_http.js”, and initiate the file:

Initiate demo_http.js:

C:\Users\Your Name>node demo_http.js

Add an HTTP Header

If the response from the HTTP server is supposed to be displayed as HTML, you should include an HTTP header with the correct content type:

Example:

add-http-header

Run Example>>

Read the Query String

The function passed into the http.createServer() has a req argument that represents the request from the client, as an object (http.IncomingMessage object).

This object has a property called “url” which holds the part of the url that comes after the domain name:

demo_http_url.js

read-jquery-string

Save the code above in a file called “demo_http_url.js” and initiate the file:

Initiate demo_http_url.js:

C:\Users\Your Name>node demo_http_url.js

Split the Query String

There are built-in modules to easily split the query string into readable parts, such as the URL module.

Example:

Split the query string into readable parts:

split-the-string-query

Initiate demo_querystring.js:

C:\Users\Your Name>node demo_querystring.js

Express Web Framework (Node.js/JavaScript)

Express .js is a popular back end web application framework for Node.js written in JavaScript.This framework helps to create robust APIs, setup middlewares to respond to HTTP requests, defines routing tables, configure dev/prod/qa environment for deployment in a much easier and cleaner approach.

For further information regarding Node JS server and Node JS modules, you can drop us an email on info@oditeksolutions.com

What OdiTek offers

Certified Developers

Deep Industry Expertise

IP Rights Agreement -Source Codes to Customers, legal compliance

NDA – Legally binding non-disclosure terms

Compliance to Software Development Quality Standards

Product Development Excellence

Dedicated Project Manager (Not billed)

Proactive Tech Support-Round the Clock

Commitment to Schedule

High performance, Secure software design

Guranteed Cost Savings & Value Addition

Consistent Achiever of Customer Happiness

Refer our Skills page:

Node.JS Development

Being one of the pioneers Node.js web application development service providers, we at OdiTek Solutions are in the business of delivering accomplished applications. Whether it is about plug-in and module development solutions or an app development requirement, we have been catering to the requirements of...

Read More

Client Testimonials

If you need additional information or have project requirements, kindly drop an email to: info@oditeksolutions.com

Latest Insights

Android Application Development Solutions with End-to-End App Support

Android application development solutions are at the core of modern enterprise mobility, enabling businesses to build scalable, secure, and integration-ready mobile applications that align with...

MT to MX Migration: A Complete Guide to Swift Message Transformation

SWIFT MT to MX migration is driving a fundamental shift in the global financial ecosystem as part of the broader ISO 20022 adoption. With SWIFT...

SSRS to Jasper Reports Migration: Redefining Flexibility in Enterprise Reporting

As reporting requirements grow more complex, many organizations relying on SSRS (SQL Server Reporting Services) are beginning to encounter a familiar challenge, the tool continues...

Crystal Reports to Jasper Reports Migration: A Strategic Approach to Modern Reporting Solutions

In today’s data-driven business landscape, timely insights can be the difference between staying ahead and falling behind. While Crystal Reports has served enterprises for decades,...

× How can I help you?