0% found this document useful (0 votes)
10 views8 pages

Nord Js

The document contains a series of questions and answers related to Node.js, covering topics such as NPM commands, core modules, HTTP requests, and database operations with MySQL. It includes programming examples for updating and deleting records, handling events, and file operations. Additionally, it compares traditional web server models with Node.js process models and explains installation steps for Node.js on Windows.

Uploaded by

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

Nord Js

The document contains a series of questions and answers related to Node.js, covering topics such as NPM commands, core modules, HTTP requests, and database operations with MySQL. It includes programming examples for updating and deleting records, handling events, and file operations. Additionally, it compares traditional web server models with Node.js process models and explains installation steps for Node.js on Windows.

Uploaded by

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

Q1) Answer the following (any Eight): [8×2=16]

a) What is the command to initialize node package manager (NPM)? Write its syntax.
Command:

sh

CopyEdit

npm init

It initializes a new Node.js project and creates a package.json file.

b) What is REPL?
REPL stands for Read-Eval-Print Loop. It is an interactive shell in Node.js that allows users to
execute JavaScript code and see immediate results.

c) List any four core modules of Node.js.

1. http – For creating HTTP servers and clients.

2. fs – File System module to handle file operations.

3. path – To work with file and directory paths.

4. events – To handle events and event-driven programming.

d) List any two methods included under path module of Node.js.

1. path.join() – Joins multiple path segments into a single path.

2. path.basename() – Returns the last portion of a path.

e) For which tasks is the File System module used?


The fs (File System) module is used for:

• Reading files (fs.readFile())

• Writing files (fs.writeFile())

• Appending data to files (fs.appendFile())

• Deleting files (fs.unlink())

f) Write a command to add dependency “express” using NPM.

sh

CopyEdit
npm install express

g) Write a command to install MySQL package using NPM.

sh

CopyEdit

npm install mysql

h) Write down steps to handle HTTP requests while creating a web server using Node.js.

1. Import the http module.

2. Create a server using http.createServer().

3. Define a function to handle incoming requests.

4. Listen on a specific port using server.listen(port).

i) Write any two advantages of Node.js.

1. Asynchronous & Event-Driven – Non-blocking operations improve performance.

2. Single Programming Language – Uses JavaScript for both client and server-side
development.

j) Write any two functions of Buffer used in Node.js.

1. Buffer.alloc(size) – Creates a buffer of specified size.

2. Buffer.from(string) – Converts a string into a buffer.

Q2) Answer the following (any Four) [4×4=16]

a) Write a Program to update table records using Node.js and MySQL database.

javascript

CopyEdit

const mysql = require('mysql');

const connection = mysql.createConnection({

host: "localhost",
user: "root",

password: "",

database: "mydatabase"

});

connection.connect();

const sql = "UPDATE users SET name = 'John Doe' WHERE id = 1";

connection.query(sql, (err, result) => {

if (err) throw err;

console.log("Record Updated!");

});

connection.end();

b) Explain Node.js Process Model with the help of a diagram.

• Node.js uses a single-threaded, event-driven architecture.

• It uses non-blocking I/O to handle multiple requests asynchronously.

• Event loop continuously checks for pending tasks and executes callbacks.

c) How does Node.js handle a file request?

1. The client sends a request for a file.

2. Node.js uses the fs module to read the file asynchronously.

3. Once the file is read, it sends the response back to the client.

d) What is the Purpose of module.exports in Node.js?

• module.exports is used to expose functions, objects, or variables from one module to


another.
e) Explain fs.readFile() method for all possible values of options.
fs.readFile(path, options, callback) reads a file asynchronously.
Options:

• {encoding: 'utf8'} → Reads as a UTF-8 string.

• {flag: 'r'} → Opens the file for reading.

Q3) Answer the following (any four) [4×4=16]

a) Write a Program that uses addListener() method of EventEmitter class.

javascript

CopyEdit

const EventEmitter = require('events');

const event = new EventEmitter();

event.addListener('greet', () => {

console.log('Hello, Event Triggered!');

event.emit('greet');

b) Write a short note on NPM.

• NPM (Node Package Manager) is used to manage packages and dependencies in


Node.js.

• It allows installing third-party modules (npm install module_name).

c) Write a Program to delete table records using Node.js and MySQL.

javascript

CopyEdit

const mysql = require('mysql');

const connection = mysql.createConnection({

host: "localhost",

user: "root",
password: "",

database: "mydatabase"

});

connection.connect();

const sql = "DELETE FROM users WHERE id = 1";

connection.query(sql, (err, result) => {

if (err) throw err;

console.log("Record Deleted!");

});

connection.end();

d) How do you install Packages locally using NPM? Explain with an example.

• Run npm install package-name inside the project directory.


Example:

sh

CopyEdit

npm install express

This installs express locally in the node_modules folder.

e) Compare Traditional Web Server Model and Node.js Process Model.


Traditional Web Server Node.js

Uses multiple threads Single-threaded

Blocking I/O Non-blocking I/O

Handles one request per thread Handles multiple requests asynchronously


Q4) Answer the following (any four) [4×4=16]

a) Write a Program to use SQL SELECT query to show data from a table using Node.js and
MySQL.

javascript

CopyEdit

const mysql = require('mysql');

const connection = mysql.createConnection({

host: "localhost",

user: "root",

password: "",

database: "mydatabase"

});

connection.connect();

const sql = "SELECT * FROM users";

connection.query(sql, (err, results) => {

if (err) throw err;

console.log(results);

});

connection.end();

b) Explain steps to install Node.js on Windows.

1. Download Node.js from the official website.

2. Run the installer (.msi file).

3. Follow the installation wizard.


4. Verify installation using node -v.

c) Write a Program to write to a file in Node.js.

javascript

CopyEdit

const fs = require('fs');

fs.writeFile('example.txt', 'Hello, Node.js!', (err) => {

if (err) throw err;

console.log('File Written Successfully!');

});

d) How to add dependency into package.json?

• Run npm install package-name --save to add a package.

e) Write a Program to calculate factorial of a given number using a function.

javascript

CopyEdit

function factorial(n) {

if (n === 0) return 1;

return n * factorial(n - 1);

console.log(factorial(5)); // Output: 120

Q5) Answer the following (any two) [2×3=6]

a) Explanation of the HTTP Server Program.

• It creates a server using http.createServer().

• The server responds with "Hello World".


• The server listens on port 8080.

b) Explain working of writeHead().

• res.writeHead(200, {'Content-Type': 'text/html'}) sets HTTP headers.

• 200 is the status code (OK).

• 'Content-Type': 'text/html' specifies the response type.

c) Explain Inheriting Events with an example.

javascript

CopyEdit

const EventEmitter = require('events');

class MyEmitter extends EventEmitter {}

const myEmitter = new MyEmitter();

myEmitter.on('event', ()

4o

You might also like