adds code

What happens when Node.js runs a file that creates a web server?

Quick answer: The http.createServer() line builds a server object, .listen(8080) starts it on port 8080, and every browser request to http://localhost:8080 triggers the handler function you passed in. When that handler calls a function from your own module — like the date-time example in this video's code — Node.js loads your module file once and reuses it for every request.

How do modules and HTTP servers work together in the video's example?

The example splits responsibility across two files, following the pattern documented in the official Node.js Modules API reference:

  • myfirstmodule.js — exports one function: exports.myDateTime = function () { return Date(); };
  • demo_module.js — requires the built-in http module and the local module (require('./myfirstmodule')), then serves its output as HTML on port 8080.

This separation matters beyond toy examples: real applications isolate database access, configuration, and business logic into modules so each part can be tested and replaced independently.

What is localhost:8080 actually telling you?

localhost means "this same machine" (the IP address 127.0.0.1) and 8080 is the TCP port the server listens on. The response arrives because res.writeHead(200, {'Content-Type': 'text/html'}) sends an HTTP status 200 with an HTML content type before the body is written. Status codes are standardized; MDN's HTTP status reference lists what each code means.

Which errors trip up beginners following this tutorial?

SymptomCauseFix
EADDRINUSEPort 8080 already taken by another program or an old instance still runningStop the old process or change .listen(8080) to another port
Cannot find module './myfirstmodule'File name mismatch or wrong folderBoth files must sit in the same directory; check spelling exactly
dt.myDateTime is not a functionFunction never attached to exportsAdd the exports.myDateTime = ... line to the module file
Browser shows nothing / hangsHandler never called res.end()Always end the response; the Node.js HTTP API docs cover the full request/response lifecycle

How do you run and stop the server?

  1. Open a terminal in the project folder.
  2. Run node demo_module.js — the process stays running while listening.
  3. Visit http://localhost:8080; refresh to see the timestamp update per request.
  4. Stop with Ctrl+C in the terminal.

Frequently Asked Questions

Why does the displayed time change on every refresh?

The handler calls dt.myDateTime() for each incoming request, so each page load evaluates the current date and time fresh — a simple demonstration of dynamic server responses versus static HTML files.

Can I require the same module in multiple files?

Yes. Node caches module exports after first load, so every file requiring './myfirstmodule' shares the same exported functions, per the modules documentation.

Is port 8080 special?

No — it is just a conventional alternative to port 80 (the default HTTP port, which usually requires admin rights). Any free port above 1024 works without elevated permissions.

Where should I put more functions I write?

In more module files, grouped by purpose, then required where needed. The npm packages-and-modules guide describes how projects grow from single files into structured package directories.

Sources



How To Create Module | Web application Node Js | Tutorial For Beginner https://youtu.be/nXUdLhoxCbw Create Your Own Modules You can create your own modules, and easily include them in your applications. The following example creates a module that returns a date and time object: Example Create a module that returns the current date and time: exports.myDateTime = function () { return Date(); }; Use the exports keyword to make properties and methods available outside the module file. Save the code above in a file called "myfirstmodule.js" Include Your Own Module Now you can include and use the module in any of your Node.js files. Example Use the module "myfirstmodule" in a Node.js file: var http = require('http'); var dt = require('./myfirstmodule'); http.createServer(function (req, res) { res.writeHead(200, {'Content-Type': 'text/html'}); res.write("The date and time are currently: " + dt.myDateTime()); res.end(); }).listen(8080); Notice that we use ./ to locate the module, that means that the module is located in the same folder as the Node.js file. Save the code above in a file called "demo_module.js", and initiate the file: Initiate demo_module.js: C:\Users\Your Name\node demo_module.js If you have followed the same steps on your computer, you will see the same result as the example: http://localhost:8080 Hello World Program | Web application Node Js | Tutorial For Beginner https://youtu.be/gDQ-jU5bo7w Software Installation | Node Js :- https://youtu.be/7gi6lgK-BIA Node Js Download Link:- https://nodejs.org/en/download Software Installation Playlist https://www.youtube.com/playlist?list=PLHeKsaIQNmlqvVEHF9mMfUPMCPtIjlARL #Subscribe the #Channel #Link :- #bansodetechsoluiotn #ajupgrading https://www.youtube.com/c/AjUpgradingBANSODETECHSOLUTION?sub_confirmation=1 IF any #Query or #Doubt #DM on #Instagram :- #bansode_ajay_2102 https://www.instagram.com/bansode_ajay_2102?r=nametag Whatsapp Channel Link https://whatsapp.com/channel/0029VaOllPkEKyZAoy7K791X
Previous Post Next Post