0% found this document useful (0 votes)
2 views4 pages

Program No 3a

Uploaded by

meghana31p
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views4 pages

Program No 3a

Uploaded by

meghana31p
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

Program No 3a Deployment of node.js and built-in node.js modules.

Source Code: Save Html program with js extension.


Assert function.

console.clear()
const assert = require('assert');

let x = 4;
let y = 5;

try {

// Checking condition
assert(x == y);
}
catch {

// Error output
console.log(
`${x} is not equal to ${y}`);}

2) Buffer function:

// Write JavaScript code here


rbuf = new Buffer(26);
var j;

for (var i = 65, j = 0; i < 90, j < 26; i++, j++) {


rbuf[j] = i ;
}

console.log( rbuf.toString('utf-8', 3, 9));


3) Console Function:
// Node.js program to demonstrate the
// console.assert() Method

// Accessing console module


const console = require('console');

// Calling console.assert()
let a = 10, b = 5;

console.assert(1 == 1, "error at 1==1");


console.assert(1 != 1, "error at 1!=1");
console.assert(3 > 9, "error at 3>9");
console.assert(1 & 6, "error at 1&6");
console.assert(0 && 9, "error at 0&&9");
console.assert(1 && 8, "error at 1&&8");
console.assert(a % b == 1, "error at a%b==1");
console.assert(a > b, "error at a>b");
console.assert(b > a, "error at b>a");

4) //File function
const fs = require("fs");
const buf = new Buffer(1024);
console.log("opening an existing file");
fs.open("input.txt", "r+", function (err, fd) {
if (err) {
return console.error(err);
}
console.log("File opened successfully!");
console.log("reading the file");

fs.read(fd, buf, 0, buf.length, 0, function (err, bytes) {


if (err) {
console.log(err);
}
console.log(bytes + " bytes read");

// Print only read bytes to avoid junk.


if (bytes > 0) {
console.log(buf.slice(0, bytes).toString());
}
});});
5) http function:
const express = require('express')
const app = express()
const port = 3000

app.get('/', (req, res) => {


res.send('Hello World!')
})

app.listen(port, () => {
console.log(`Example app listening on port ${port}`)
})

6) Path function:
// Accessing dns module
const dns = require('dns');

// Reading IP address of the current host


// and printing it to the console
console.log(dns.getServers());

Output:

You might also like