Node-JS-Server-&-Node-JS-Modules

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


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...

more

Client Testimonials

We had a tough deadline to launch our .Net based application that processes a lot of data, and got very frustrated with our development agency we hired. Fortunately we got Oditek, and they took over seamlessly the product development, launched the app & continued feature development. Just awesome!

Neal Bonrud

Co-Founder – SubScreener, USA

They were very attentive to our needs as clients and went out of the way to make sure our projects were taken care of. They were always able to get projects done in the specifications we requested. They are passionate about getting things done; I would definitely recommend them to lead any IT projects.

Dann Manahan

Sr VP Technology- 1031 Crowd Funding

I worked with OdiTek on few high profile banking application projects. They did a fantastic job with web applications & manual testing on the VAS apps for two leading banks of UK that included rigorous UAT phases. I recommend them for any application development where security matters.

Clive Shirley

CTO- Smarta, UK

OdiTek is our extended team who works on our key software projects. They are dependable, good in collaboration and technically very much to the level what we expect a global team should be. They had transformed our web applications, CRM and added mobility to existing business platforms here.

Matt Berry

IT Manager- First Option Online

It's been more than 4 years now that we are working with OdiTek on our cloud based web product development. It's been amazing working together, they are very competent on designing scalable, high performance apps. Their technical support is outstanding to say the least, even at odd hours.

Brad Taylor

CEO- BluesummitTech, USA

I am a fan of Team OdiTek since 2014 and have worked on many product development projects together. Specially worth mentioning their deliveries on VAS Banking web application development & manual testing services for Smarta, UK. They are highly skilled & a professional team to work with.

Tom Bowden

Digital Propositions - HSBC, London

OdiTek has been working on our Integrated Web-scale Mobile Platform i.e. Optimal Health since 2014. They are very professional and takes care of the requirements meticulously. They are technically very sound and sincere in ensuring quality & performance. Wonderful working with them!

Catherine Lim

COO- Medilink Global Sdn Bdh

You can trust the team, with minimum supervision you get the work done. They are honest, professional & committed to schedule & quality. I had been successfully running 3 business applications designed, developed and maintained by Oditek developers. It’s been a pleasure working with them.

Scott Evans

CEO- Pink Storage, UK

OdiTek has been working in custom software development, including services for test automation. Many of them have worked with me in 2009-10 when I was R&D Manager in NetHawk India. They have great enthusiasm & a passion to excel in bringing customer success. Their work has been very impressive.

Karen Hamber

Senior Product Manager- Skype

It's amazing to see these guys are turning their experience into a global delivery excellence at OdiTek. I am sure their past large scale product development experience will be handy to product companies. I would always recommend Oditek for software development, especially performance-driven solutions.

Juha Marjeta

Opti Automation Oyj

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

×