Day 9 of 100days of code.

Day 9 of 100days of code.

Β·

2 min read

As a frontend developer venturing into backend development with a solid foundation in frontend technologies, I am embarking on a journey to deepen my understanding of backend principles and practices. Lets GooooπŸ’ͺπŸΌπŸš€

Fundamentals of nodeJS

Creating a custom Module

A module is a script file which allows us to export some values and those values can be used by other script files by simply importing it. Each script file in nodeJS is a module. readline, http, fs, url are all core modules provided by nodeJS.

Custom modules are basically the modules we developer create ourselves.

In the code base,

Lets create a new folder modules and inside the modules folder we'll keep all our custom modules. lets transform replaceHtml() function into a module.

Inside the modules folder, lets create replaceHtml.js file. Lets update the file with the content of replaceHtml() function.

We want the above function to be available in other script files also and for that we need to export this function by using the module object which is a global object in nodeJS and on that we can call the export. πŸ‘‡πŸΌ

We're exporting the function definition using the module.exports.

In order to be able to use this function in our app.js file, we need to import this function in our app.js file . To import a module, we use the require() method. Since we're using a custom module, we need to pass the path of that custom module file. The function will return an object so lets store the function in a variableπŸ‘‡πŸΌ

We are replacing the old replaceHtml() function with our custom module since we already used the variable name all over our code base. Lets comment out or remove the previous replaceHtml() function from our code base to avoid conflict. πŸ‘‡πŸΌ

Lets restart the server to see the response in the web page. πŸ‘‡πŸΌ

After using our custom module, the page still works perfectly. πŸ‘πŸ»

Next: Understanding event-driven architecture.

See you soon😊🫑✌🏼...

Β