node.js provides a complete solution for server-side applications, such as web platforms. It can communicate with other systems, like a database, LDAP or any other legacy application. There are many scenarios we could choose to implement node.js.
node.js provides modules to help any development project, this helps to reduce development time because it negates the need to write many lines of code, because there are tens of thousands of modules in the npm registry to help in addressing common recuring problems.
It is also possible to develop your own module for node.js. For instance, consider you have a function and you may have a requirement to call this functions many times in many files across your application. We could write a function once in a module once in a module , then it will be called by simply attaching the module to a file.
Create a Simple Module
I'll illustrate how to write a very simple function then it will be exported as a module. It is not going to be a particulalry earth shattering and useful module but it will be suffice to prove a point.
This file is basically going to be our fab and groovy module we want to call.
var myModule = require('./GreetingModule.js'); var result = myModule.greeting('Gary Woodfine'); console.log(result);
The above code essentially references our previously created module, we use the require keyword, which needs the full path of the module. './' means the module has the same location as the caller.
We can now go to the command line and navigate to our directory where out files are kept and call:
In my case I just use webstorm in the terminal window, which displayed the following result.
- Book Review: Continuous Architecture in Practice - March 20, 2023
- Book Review: Escaping the Build Trap - March 2, 2023
- How to create Github profile page - February 26, 2023