Skip to content
Start Reading
Node.js

Node.js Interview Questions

1. What is Node.js?

Node.js is a JavaScript runtime built on Chrome's V8 JavaScript engine. Node.js uses an event-driven, non-blocking I/O model that makes it lightweight and efficient. Node.js' package ecosystem, npm, is the largest ecosystem of open source libraries in the world.

2. What is web server ?

A web server is a software and hardware that uses HTTP (Hypertext Transfer Protocol) and other protocols to respond to client requests made over the World Wide Web. A web server can be a computer system that hosts a website or a system that hosts specific content on the internet. The man job of a web server is to store, process and deliver web pages to users. Beside HTTP, web servers can also support other protocols such as FTP (File Transfer Protocol) and SMTP (Simple Mail Transfer Protocol).

3. What is node jS http module ?

Node.js has a built-in module called HTTP, which allows Node.js to transfer data over the Hyper Text Transfer Protocol (HTTP).

Example

var http = require('http')

http
	.createServer(function (req, res) {
		res.write('Hello World!') //write a response to the client
		res.end() //end the response
	})
	.listen(8080) //the server object listens on port 8080

So when we visit http://localhost:8080/ in our browser, we will see the text "Hello World!".

4. What is the purpose of HTTP Header

An HTTP header is a field of an HTTP request or response that passes additional context and metadata about the request or response. For example, a request message can use headers to indicate it's preferred media formats, while a response can use header to indicate the media format of the returned body.

Example

const http = require('http')

http
	.createServer(function (req, res) {
		res.writeHead(200, { 'Content-Type': 'text/html' })
		res.write('Hello World!') //write a response to the client
		res.end() //end the response
	})
	.listen(8080) //the server object listens on port 8080
5. How nodejs process works ?

NodeJS is a single threaded process. It means that it can only do one thing at a time that is why it is called as a single threaded process. But it can do many things at a time by using asynchronous programming and event loop.

When request comes from the client, it accept this request and pass though the worker thead for process necessary things. After that it will send the response to the client. In this process, it will not wait for the response from the worker thread. It will continue to accept the next request. This is the reason why NodeJS is called as a single threaded process but non-blocking I/O.

6. What are the features of nodejs
  • Asynchronous and Event Driven
  • Fast code execution (V8 Engine).
  • Single Threaded but Highly Scalable
  • No Buffering
  • Large Community Support
7. What is purpose of node js url module

The URL module splits up a web address into readable parts.

Syntax

const url = require('url')

const myURL = new URL('https://example.org/foo')

console.log(myURL.href) // https://example.org/foo

console.log(myURL.protocol) // https:

console.log(myURL.host) // example.org

console.log(myURL.hostname) // example.org

console.log(myURL.port) // 443

console.log(myURL.pathname) // /foo

console.log(myURL.search) // ''

console.log(myURL.searchParams) // URLSearchParams {}
8. What is Buffers data type?

The Buffer class in Node. js is designed to handle raw binary data. Each buffer corresponds to some raw memory allocated outside V8. Buffers act somewhat like arrays of integers, but aren't resizable and have a whole bunch of methods specifically for binary data.

const buf = Buffer.from('Hello World', 'utf8')

console.log(buf) // <Buffer 48 65 6c 6c 6f 20 57 6f 72 6c 64>

console.log(buf.toString()) // Hello World

console.log(buf.toString('hex')) // 48656c6c6f20576f726c64

console.log(buf.toString('base64')) // SGVsbG8gV29ybGQ=
9. What is EventEmitter in Node.js?

The EventEmitter is a class which is available to us by importing the events module. It is used to handle events and emit events. It is a built-in module in Node.js.

const EventEmitter = require('events')

const eventEmitter = new EventEmitter()

setTimeout(() => {
	eventEmitter.emit('tutorial', 1, 2)
}, 3000)

eventEmitter.on('tutorial', (num1, num2) => {
	console.log(num1 + num2)
})

The above code will output 3 after 3 seconds.

10. What is process events in nodejs

Process object provides information about the current or running process. It is a global object and can be accessed from anywhere. It is an instance of EventEmitter class. It is an event emitter object.

11. How can we read a file content in nodejs

Using fs module we can read a file content in nodejs. We can use readFile() method to read a file content. It takes two arguments, first is the file path and second is the callback function. The callback function takes two arguments, first is error and second is the data. If there is no error then data will be the file content.

const fs = require('fs')

fs.readFile('file.txt', (err, data) => {
	if (err) throw err
	console.log(data)
})
12. What Is a Node.js Stream?

Streams are a built-in feature in Node.js and represent asynchronous flow of data. Streams are also a way to handle reading and/or writing files. A Node.js stream can help process large files larger than the free memory of your computer, since it processes the data in small chunks.

13. What is HTTP ?

HTTP (Hypertext Transfer Protocol) is a protocol that is used to transfer data between a client and a server. HTTP is a stateless protocol, which means that the server does not keep any data (state) between two requests. There are a secure variant of HTTP called HTTPS (Hypertext Transfer Protocol Secure) that uses SSL/TLS to encrypt the communication between the client and the server.

14. What is cluster in nodejs

Cluster is a module in Node.js that allows us to create multiple instances of a server. This is useful when we want to take advantage of multi-core systems. It allows us to create child processes that all share server ports.

const cluster = require('cluster')
const express = require('express')
// Constants
const PORT = 8080

// App
const app = express()
app.get('/', (req, res) => {
	res.send('Hello world\n')
})

if (cluster.isMaster) {
	// Count the machine's CPUs
	const cpuCount = require('os').cpus().length

	// Create a worker for each CPU
	for (let i = 0; i < cpuCount; i += 1) {
		cluster.fork()
	}

	// Listen for dying workers
	cluster.on('exit', (worker) => {
		// Replace the dead worker,
		// we're not sentimental
		console.log('Worker %d died :(', worker.id)
		cluster.fork()
	})

	// Code to run if we're in a worker process
} else {
	app.listen(PORT)
	console.log('Running on http://localhost:' + PORT)
}
15. What is difference between concurrency and parallelism ?

Concurrency is when two or more tasks can start, run, and complete in overlapping time periods. It doesn't necessarily mean they'll ever both be running at the same instant. For example, multitasking on a single-core machine.

Parallelism is when tasks literally run at the same time, e.g., on a multicore processor.

16. What is JWT ?

JWT, or JSON Web Token, is an open standard used to share security information between two parties — a client and a server. Each JWT contains encoded JSON objects, including a set of claims. JWTs are signed using a cryptographic algorithm to ensure that the claims cannot be altered after the token is issued.

17. what is middleware in node js

Middleware is a function that has access to the request object (req), the response object (res), and the next middleware function in the application’s request-response cycle. The next middleware function is commonly denoted by a variable named next.

Express Middleware

const express = require('express')
const app = express()

app.use((req, res, next) => {
	console.log('Time:', Date.now())
	next()
})

app.get('/', (req, res) => {
	res.send('Hello World!')
})

app.listen(3000)
18. What is use of middleware in node js ?

Middleware can be used to perform the following tasks:

  • Execute any code.
  • Make changes to the request and the response objects.
  • End the request-response cycle.
  • Call the next middleware in the stack.
19. what is the use of helmet in node js

Helmet helps you secure your Express apps by setting various HTTP headers. It's not a silver bullet, but it can help! Helmet is actually just a collection of smaller middleware functions that set security-related HTTP response headers:

How to use

const express = require('express')
const helmet = require('helmet')

const app = express()

app.use(helmet.contentSecurityPolicy())
app.use(helmet.crossOriginEmbedderPolicy())
app.use(helmet.crossOriginOpenerPolicy())
app.use(helmet.crossOriginResourcePolicy())
app.use(helmet.dnsPrefetchControl())
app.use(helmet.expectCt())
app.use(helmet.frameguard())
app.use(helmet.hidePoweredBy())
app.use(helmet.hsts())
app.use(helmet.ieNoOpen())
app.use(helmet.noSniff())
app.use(helmet.originAgentCluster())
app.use(helmet.permittedCrossDomainPolicies())
app.use(helmet.referrerPolicy())
app.use(helmet.xssFilter())

app.listen(3000)
20. what is rest api in nodejs?

REST APIs are used to access and manipulate data using a common set of stateless operations. These operations are integral to the HTTP protocol and represent essential create, read, update, and delete (CRUD) functionality. REST APIs are designed to be lightweight and fast, and are often used to provide public access to data for third-party developers.

21. What's the difference between req.params and req.query?

The first difference between query and path parameters is their position in the URL. While the query parameters appear on the right side of the '? ' in the URL, path parameters come before the question mark sign. Secondly, the query parameters are used to sort/filter resources.

22. What is routing in nodejs ?

Routing refers to how an application's endpoints (URIs) respond to client requests. For an introduction to routing, see Basic routing.

23. What is child process in nodejs ?

NodeJS in single threaded. It means that it can only do one thing at a time. If you want to do multiple things at the same time, you need to use child process. For example, if you want to run a long running task in the background, you can use child process.

24. what are http methods ?
  • GET: The GET method requests a representation of the specified resource. Requests using GET should only retrieve data.

  • POST: The POST method is used to submit an entity to the specified resource, often causing a change in state or side effects on the server.

  • PUT: The PUT method replaces all current representations of the target resource with the request payload.

  • DELETE: The DELETE method deletes the specified resource.

  • PATCH: The PATCH method is used to apply partial modifications to a resource.

25. What is crypto in node js?

Crypto is a module in Node. js which deals with an algorithm that performs data encryption and decryption. This is used for security purpose like user authentication where storing the password in Database in the encrypted form. Crypto module provides set of classes like hash, HMAC, cipher, decipher, sign, and verify.

26. What is ftp protocol ?

FTP (File Transfer Protocol) is a protocol that is used to transfer files between a client and a server. FTP is a stateful protocol, which means that the server keeps the state between two requests. There are a secure variant of FTP called FTPS (File Transfer Protocol Secure) that uses SSL/TLS to encrypt the communication between the client and the server.

27. What is thread in computer ?

A thread is an execution context, which is all the information a CPU needs to execute a stream of instructions.

28. How Node.js works

NodeJS is a runtime environment for executing JavaScript code. It is built on top of the V8 JavaScript engine, the same engine that powers Google Chrome. NodeJS uses an event-driven, non-blocking I/O model that makes it lightweight and efficient, perfect for data-intensive real-time applications that run across distributed devices.

For example when a user makes a request to a web server, the server will start processing the request and return a response. While the server is processing the request, it can still accept and process other requests. This is made possible by the event loop. The event loop allows NodeJS to perform non-blocking I/O operations even though JavaScript is single-threaded.

29. What is cpu intensive tasks in NodeJS ?

In NodeJS CPU intensive tasks are those tasks which process

30. Is node js really single threaded ?

No. NodeJS is not single threaded. The nodejs event loop is process single threaded, But the async blocking I/O operation are delegated to the worker threads.

31. What Can Node.js Do?
  • Node.js can generate dynamic page content
  • NodeJS can create, open, read, write, delete, and close files on the server.
  • NodeJS can collect form data
  • Can add , delete, modify data in your database.
Last updated on February 27, 2023