There are are different types of module you can typically load by calling the require function program.js const utility = require( './utility') It exposes the imported(the module been required) modules exported objects and makes them usable withing the requiring module. Require is a function used for loading a module into another module. #NODEJS REQUIRE CODE#Drawing from our previous code sample, and can also be written as exports.add and exports.subtract respectively. exportsĮxports is a convenience method for module.exports as it takes lesser keystroke and is also succinct. When utility.js is required in another module, the functions can be called within the requiring module. Indicating that the functions have being successfully exposed. In Node every file is considered a module and before each file (module) is executed, it's wrapped within a Module Wrapper function which exposes the following variables/arguments module, exports, require, filename, dirname and looks something like ( function( exports, require, module, _filename, _dirname). NB: For the rest of this article, i'd be using Node in place of Node.js mostly because it takes lesser keystroke and that is pretty what it's called in the ecosystem. Typically, a program in most cases is composed of different modules. Understanding how the module system works is integral for writing maintainable Node.js applications.Ī module is a piece of a program with abstracted functionality needed by the entire program. In Node.js we are given a module system which enable application developers to abstract logic into modules, export the modules to be used else where in our application. To keep our application maintainable it's a good practice to split the different logic into smaller pieces that have specific responsibilities and then import these pieces of logic into the main application for reuse. Succinctly, Node.js as a platform provides an environment outside of the traditional web browser for executing JavaScript code (It's important to note here that Node.js was created for building network applications using JavaScript).Ī typical production ready Node.js application is feature intensive (the endpoints of Restful API's are typical example where each endpoint is a feature and have a unique responsibility) and demands some amount of logic which in turn demands that you code lots of functions that are responsible for realizing the different features that make up your application. Node.js is a JavaScript run-time built on top of the chrome v8 engine.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |