Explanation: From javascripttutorial: reference
Explanation: From flaviocopes docs: reference
Explanation: From official docs: reference
Explanation: From flaviocopes docs: reference
Explanation: From official docs: reference To minimize memory costs, when possible prefer streaming via fs.createReadStream().
Explanation: From official docs: reference
Explanation: From official docs: reference
Explanation: From official docs: reference
Explanation: From official docs: reference
Explanation: From official docs: reference
Explanation: From coderrocketfuel docs: reference
Explanation: From official docs: reference
Explanation: From official docs: reference
Explanation: From official docs: reference
Explanation: From official docs: reference
Explanation: From official docs: reference
require('child_process').fork('script.js');
Explanation: From official docs: reference
Explanation: Because the EventEmitter is already in scope. No need to create new one.
Explanation: process.stdout is Buffer type.
__pathname
__location
__flder
__filename
Reference: From official docs: reference
const http = require('http');
const hostname = '127.0.0.1'; const port = 3000;
const server = http.createServer((req, res) => {
res.statusCode = 200; res.setHeader("Content-Type", "text/plain"); res.end("Hello World\n");
});
server.listen(port, hostname, () => { console.log(`server running at http://${hostname}:${port}/`); });
Explanation: From official docs: reference
Explanation: From official docs: reference
const fs = require('fs'); const os = require('os');
const system = os.platform(); const user = os.userInfo().username;
fs.appendFile('hello.txt', `Hello ${user} on ${system}`, (err) => { if (err) throw err; console.log('The data was appended to file!');}
);
Explanation: From official docs: reference
console.log(arguments);
Explanation:: Reference Article The output of executing console.log(arguments); in Node.js will be a ReferenceError: arguments is not defined. The arguments object is not available in Node.js, as it is a specific feature of the browser JavaScript environment. In browsers, the arguments object is an array-like object that contains all of the arguments that were passed to a function. However, in Node.js, there is no arguments object, and the only way to access the arguments that were passed to a function is to explicitly declare them in the function’s parameter list.
exports, __filename, __dirname
exports, process, require, module, __filename, __dirname
exports, module, __filename, __dirname
exports, require, module, __filename, __dirname
Explanation: From official docs: reference
Explanation: _process is an global object and act like a bridge, the others aren’t
Explanation: https://www.geeksforgeeks.org/why-node-js-is-a-single-threaded-language/
Explanation: From official docs: https://nodejs.org/api/cluster.html#cluster_cluster
Explanation: From official docs: https://nodejs.org/api/readline.html#readline_example_read_file_stream_line_by_line
not
a Node global object?Explanation: exports
may appear to be global but is not. Refrence
Explanation: From official docs: reference
// File: person.js
exports.name = "Jane";
// File: index.js
const person = require('./person.js');
console.log(person);
{'Jane'}
{ name: 'Jane' }
{}
Jane
// File: person.js
exports = "John";
// File: index.js
const person = require('./person.js');
console.log(person);
John
Undefined
{'John'}
{}
From the article: Making a Testing Framework in Node.js (Without any External Libraries)
require(something)
, where will Node.js attempt to resolve(something)
?const http = require('http');
const hostname = '127.0.0.1';
const port = 3000;
const server = http.createServer((req, res) => {
res.statusCode = 200;
res.setHeader('Content-Type', 'text/plain');
res.end('Hello World\n');
});
server.listen(port, hostname, () => {
console.log(`Server running at http://${hostname}:${port}/`);
});
server running at http://127.0.0.1:3000
server running at port 3000
server running at http://localhost:3000/
server running at http://localhost:4000/
Hello World
Q78. What is the primary function of the npm command in Node.js development?
Explanation: The npm command is the Node Package Manager and is primarily used to manage packages and dependencies for Node.js projects. It allows developers to install, update, and manage packages from the npm registry.
Q79. In Node.js, how can you handle asynchronous operations effectively to avoid callback hell?
Explanation: To handle asynchronous operations effectively and avoid callback hell, it’s recommended to use Promises, async/await, or libraries like async.js. These techniques provide a more structured and readable way to work with asynchronous code.
Q80. Which core module in Node.js can be used to create web servers?
Explanation: The http core module in Node.js can be used to create web servers. It provides the necessary functionality to handle HTTP requests and responses, making it possible to create web applications and APIs.
Q81. What is the purpose of the os module in Node.js?
Explanation: The os module in Node.js is used to provide information about the host operating system. It offers functions to access details about the CPU, memory, and network interfaces, making it useful for system-related tasks.
Q82. How can you serve static files, such as HTML, CSS, and images, in a Node.js web application?
Explanation: To serve static files in a Node.js web application, you can use middleware like express.static in combination with the Express.js framework. This middleware simplifies the process of serving HTML, CSS, images, and other static assets.
Q84. How can you terminate a Node.js application programmatically?
Explanation : To terminate a Node.js application, you can either send a SIGINT signal or call process.exit() programmatically.
Q85. What is the purpose of the child_process module in Node.js?
Explanation : The child_process module is used for creating and managing child processes to run external commands or scripts independently.
Q86. What is the primary purpose of the cluster module in Node.js?
Explanation : The primary purpose of the cluster module in Node.js is to utilize multicore systems efficiently by forking multiple Node.js processes to distribute workloads.
Q87. Which method of the fs module in Node.js is used to check if a file exists asynchronously?
Explanation: The fs.access method in Node.js is used to check if a file exists asynchronously. It is a recommended way to check for the existence of a file as it does not throw an error if the file doesn’t exist, and it’s more efficient than using fs.exists or fs.stat. If the file exists, the callback function will be called with no error. If the file doesn’t exist, the callback will be called with an error.
Q88. Which core module in Node.js is used for network programming and creating network applications?
Explanation: The net core module in Node.js is used for network programming and creating network applications. It provides the necessary functionality for creating both TCP and Unix socket servers and clients.
Q89. What is the purpose of the util.promisify method in Node.js?
Explanation: The util.promisify method in Node.js is used to convert a callback-style function into a function that returns a Promise. It simplifies working with asynchronous functions by allowing you to use async/await syntax and Promise-based error handling.
Q90. Which Node.js module provides an interface for interacting with the file system, including reading and writing files?
Explanation: The fs module in Node.js provides an interface for interacting with the file system. It allows you to perform various file-related operations, including reading and writing files, creating directories, and more.