Node JS Modules
Node JS Modules
EXPERIMENT 1
AIM:
var fs = require("fs");
var data = fs.readFileSync('input.txt');
$ node main.js
AIM:
To demonstrate the event emitter pattern .
function c2() {
console.log('yet another event occurred!');
}
1
myEmitter.on('eventOne', c1); // Register for eventOne
myEmitter.on('eventOne', c2); // Register for eventOne
When the event „eventOne‟ is emitted, both the above callbacks should
be invoked.
myEmitter.emit('eventOne');
AIM:
To demonstrate the use of defer execution of a function
// Raising FirstEvent
em.emit('FirstEvent', 'This is my first Node.js event emitter example.');
AIM:
if (thisIsTrue) {
response.send('All is good!');
cb(null, response)
} else {
response.send('ERROR! ERROR!');
2
return cb("THIS ISN'T TRUE!");
}
AIM:
To demonstrate the use Block escape event loop
const fs = require('fs');
function someAsyncOperation(callback) { //
Assume this takes 95ms to complete
fs.readFile('/path/to/file', callback);
}
setTimeout(() => {
const delay = Date.now() - timeoutScheduled;
MODULE III
EXPERIMENT 1
AIM:
3
// Require the given module
var fs = require('fs');
// Use statSync() method to store the returned
// instance into variable named stats
var stats =
fs.statSync("/Users/divyarani/Documents/geekforgeeks/geeks.js");
// Use isFile() method to log the result to screen
console.log('is file ? ' + stats.isFile());
var stats = fs.statSync("/Users/divyarani/Documents/geekforgeeks/geek");
// Use isDirectory() method to log the result to screen
console.log('is directory ? ' + stats.isDirectory());
Output
is file ? true is
directory ? true
AIM:
To demonstrate the how to read, write, & close file
app.js
var fs = require("fs");
fs.readFile("temp.txt", function(err, buf) {
console.log(buf.toString());
});
var fs = require("fs");
This line does the job of importing the fs package and allowing us to utilize
it within our own code.
AIM:
Demonstrate how to read data in SQL using node js
The following select.js program selects all data from the todos table of the
todoapp database:
4
let mysql = require('mysql'); let
config = require('./config.js');
connection.end();
5
>node select2.js
[ RowDataPacket { id: 1, title: 'Learn how to insert a new row', completed:
1 },
RowDataPacket { id: 4, title: 'It should work perfectly', completed: 1 } ]