EventEmitter is a Node native class containing a set of methods to operate on the events.
#node#module#events
constEventEmitter=require('events');constemitter=newEventEmitter();// Set an event listeneremitter.on('userSubscribed',function(eventArg){console.log("User subscribed at ",eventArg.date);// Do something when user subscribes});// Raise an event and pass data as the second argumentemitter.emit('userSubscribed',{date:Date.now()})
Node comes with a full support for setting up a HTTP server. With a few lines of code you can build a simple web server.
#node#module#http
// Load HTTP moduleconsthttp=require('http');// Create an instance of a serverconstserver=http.createServer((req,res)=>{// Set a routeif(req.url==='/'){// Send back the response backres.write("Node HTTP module in action");res.end();}});// Start the server on a port 3000server.listen(3000);console.log('Server listening on port 3000');
Each file in your Node application represents a module and it has its own scope. Therefore to access data or functionality from another file/module you need to make them public within your application. To do that use Export method on the Module object.
#node#module
varsettings={colour:"green",active:true,number:43};module.exports.data=settings;// This exports an object containing the settings object// When exporting a single function or an object you can simplify the export to the following.// module.exports = settings;
Global object is an equivalent of the Window object available in the browser environment. The main difference between the two is that new variables and functions created in the top scope of the application are added to the Window object in the browser environment but not in Node. In Node, they are scoped to the file (module) they have been declared in.
OS module is a native Node module giving you access to operating system information like memory, architecture, uptime and many more.
#node#module
constos=require('os');// Node native modulefunctionlogOsData(){console.log(`Architecture: ${os.arch()}`);console.log(`Total memory: ${os.totalmem()}`);console.log(`Free memory: ${os.freemem()} `);console.log(`Platform: ${os.platform()} `);console.log(`Type: ${os.type()} `);console.log(`Uptime: ${os.uptime()} `);// Uncomment the line below to log full OS object in the console.// console.log(os); }logOsData();
Parse a method belonging to the Path native Node module. A parsed path returns an object containing directory, module base name, extension and the file name.
Read directory (readdir) is a method belonging to the Node native file system module (fs). reddir is an asynchronous method and it used a javascript promise to return an error or an array with a list of filenames located in the given directory.
Require is a Node specific function letting you add exported functionality and/or data from other modules. The function takes an argument for the module location. The ".js" file extension is optional. It's also a good practice to use constants when assigning required modules.
#node#module
constsettings=require('./settings.js');// This works the same wayconstsettings=require('./settings');