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?
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?
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:
2. NPM Modules
3. Local 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:
NPM modules are third-party modules that you can use only after installing them. To name a few:
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.
The exports keyword gives you the chance to “export” your objects and methods.
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:
Now your application has access to the HTTP module, and is able to create a server:
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:
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:
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:
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:
Save the code above in a file called "demo_http_url.js" and initiate the file:
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.
Split the query string into readable parts:
C:\Users\Your Name>node demo_querystring.js
For further information regarding Node JS server and Node JS modules, you can drop us an email on firstname.lastname@example.org
What Oditek offers
Refer our Skills page:
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!
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.
OdiTek has been very impressive in delivering a C++ based network security product migration from Linux to Windows. Honestly, we didn’t expect the speed and competencies to get the migration project delivered absolutely on time as the existing product was complex. Technically team is very experienced.
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.
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.
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.
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.
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!
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.
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.
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.