What is Node.js?
Node.js is a JavaScript engine used for executing JavaScript code outside the browser. It is normally used to build the backend of the application and is highly scalable.
What is the difference between Node.js and JavaScript?
JavaScript is a scripting language whereas Node.js is an engine that provides the runtime environment to run JavaScript code.
Here we have difference table between Node.js and JavaScript
Is Node.js single-threaded?
Yes, Node.js is single-threaded by default. However, it utilizes event-driven architecture and non-blocking I/O operations to handle multiple concurrent requests efficiently, enabling scalability and high performance in applications.
What kind of API function is supported by Node.js?
There are two types of API functions supported by Node.js:
- Synchronous: These API functions are used for blocking code.
- Asynchronous: These API functions are used for non-blocking code.
What is nodejs Modue
In Node.js Application, a Module can be considered as a block of code that provide a simple or complex functionality that can communicate with external application. Modules can be organized in a single file or a collection of multiple files/folders. Modules are useful because of their reusability and ability to reduce the complexity of code into smaller pieces. Some examples of modules are. http, fs, os, path, etc.
Include Modules
To include a module, use the require() function with the name of the module:
var http = require('http');
Now your application has access to the HTTP module, and is able to create a server:
http.createServer(function (req, res) {
res.writeHead(200, {'Content-Type': 'text/html'});
res.end('Hello World!');
}).listen(8080);
Create Your Own Modules
You can create your own modules, and easily include them in your applications.
The following example creates a module that returns a date and time object:
exports.myDateTime = function () {
return Date();
};
Include Your Own Module
Now you can include and use the module in any of your Node.js files.
var http = require('http');
var dt = require('./myfirstmodule');
http.createServer(function (req, res) {
res.writeHead(200, {'Content-Type': 'text/html'});
res.write("The date and time are currently: " + dt.myDateTime());
res.end();
}).listen(8080);
adjust ur file name and path while using ur module
What is npm and its advantages?
npm (Node Package Manager)
- Definition: npm is a package manager for JavaScript. It is the default package manager for the JavaScript runtime environment Node.js.
- Purpose: npm provides a way to manage dependencies for your projects. It allows developers to install, share, and distribute code in the form of packages (or modules), manage project dependencies, and even publish their own packages to the npm registry.
- Functions:
- Install packages (
npm install package-name) - Manage versioning of packages
- Handle dependencies for your project
- Publish packages to the npm registry
What are some of the most commonly used libraries in Node.js?
There are two commonly used libraries in Node.js:
- ExpressJS - Express is a flexible Node.js web application framework that provides a wide set of features to develop web and mobile applications.
- Mongoose - Mongoose is also a Node.js web application framework that makes it easy to connect an application to a database.
What is the command used to import external libraries?
The “require” command is used for importing external libraries. For example - “var http=require (“HTTP”).” This will load the HTTP library and the single exported object through the HTTP variable.
Now that we have covered some of the important beginner-level Node.js interview questions let us look at some of the intermediate-level Node.js interview questions.
var http = require('http');
What does event-driven programming mean?
An event-driven programming approach uses events to trigger various functions. An event can be anything, such as typing a key or clicking a mouse button. A call-back function is already registered with the element executes whenever an event is triggered.
What is an Event Loop in Node.js?
Event loops handle asynchronous callbacks in Node.js. It is the foundation of the non-blocking input/output in Node.js, making it one of the most important environmental features.
What is the package.json file?
The package.json file is the heart of a Node.js system. This file holds the metadata for a particular project. The package.json file is found in the root directory of any Node application or module
This is what a package.json file looks like immediately after creating a Node.js project using the command: npm init
they conatain all the information about package install through npm...
The URL Module
The URL module in Node.js provides various utilities for URL resolution and parsing. It is a built-in module that helps split up the web address into a readable format.
const url = require('url');
// Example URL to parse
const urlString = 'https://blog-mern-cfpm.onrender.com/';
// Parsing the URL
const parsedUrl = new URL(urlString);
// Accessing components of the URL
console.log('Protocol:', parsedUrl.protocol); // Output: Protocol: https:
console.log('Hostname:', parsedUrl.hostname); // Output: Hostname: blog-mern-cfpm.onrender.com
console.log('Pathname:', parsedUrl.pathname); // Output: Pathname: /
console.log('Port:', parsedUrl.port); // Output: Port: (empty string; default port for HTTPS)
console.log('Query:', parsedUrl.searchParams.toString()); // Output: Query: (empty string; no query parameters)
// Resolving a relative URL
const resolvedUrl = url.resolve('https://blog-mern-cfpm.onrender.com/', '/about');
console.log('Resolved URL:', resolvedUrl); // Output: Resolved URL: https://blog-mern-cfpm.onrender.com/about
