FALLSEM2020-21 CSE3002 ETH VL2020210106412 Reference Material I 29-Oct-2020 NodeJS Intro
FALLSEM2020-21 CSE3002 ETH VL2020210106412 Reference Material I 29-Oct-2020 NodeJS Intro
js
Events and Call backs
Node.js Modules
• A set of functions you want to include in your application.
• Node.js has a set of built-in modules which you can use without any further
installation.
• Include Modules
• To include a module, use the require() function with the name of the
module:
exports.myDateTime = function ()
{
return Date();
};
To include a module,
use the require() function with the name of the module:
Now your application has access to the HTTP module, and is able to create a
server:
Include Your Own Module
var http = require('http');
var dt = require('./myfirstmodule');
var fs = require('fs');
var rs = fs.createReadStream('./demofile.txt');
rs.on('open', function () {
console.log('The file is open');
});
Events Module
• To include the built-in Events module use the require() method.
• In addition, all event properties and methods are an instance of an
EventEmitter object.
• To be able to access these properties and methods, create an EventEmitter
object:
emitter.listeners(event) Returns a copy of the array of listeners for the specified event.
emitter.emit(event[, arg1][, arg2][, ...]) Raise the specified events with the supplied arguments.