NodeJs Interview Questions
Node.js has over 450K modules as of this writing, and is one of the popular frameworks for fast and robust frameworks.
Typically they are implemented with microservices.
Below are some NodeJs interview questions, you need to prepare
1. Have you incurred any memory leak issues in Node.js
2. Explain what is Exception Handling you used in Node.js
Node.js exception handling is different than traditional Java based exception handling, there are multiple ways exception can be handled based on the need.
Overall there are two types of Exception
4. Design Patterns used in node js
Typically they are implemented with microservices.
Below are some NodeJs interview questions, you need to prepare
1. Have you incurred any memory leak issues in Node.js
- memory leaks occur where there are short lived objects assoicated with a long term object
- e.g
const req = new Map()
app.get("/", (req,res_ => {
request.set(req.id, req)
res.send(200).send("hello world")
}
Identify it using Sample Heap Profiler.
For more info refer
2. Explain what is Exception Handling you used in Node.js
Node.js exception handling is different than traditional Java based exception handling, there are multiple ways exception can be handled based on the need.
Overall there are two types of Exception
- System Exceptions
- Logical Exceptions
There are multiple ways to handle the exception
- try /catch
- The easiest way is try/catch, blog, but things will get tricky with callback functions
- throw an error - to throw an error
- Any of the functions can throw an error, when you throw an error it becomes an exception
- retry - to avoid error
- Instead of throwing an exception, you can just retry again, for example, to make an best
- callback - throw errors in callback
- Call back can return an error, which can be handled by the caller
- crash the application for some system errors - like db failure
- like for db failure, its better to crash the application
- send errors to user if logic
- logical errors need to be sent back to the users
- Events for occasional errors for logging
- send events, incase of say rx observable or looping through and only one of them has an error
3. explain node.js Single-threaded model
Only the event loop thread which is a listener to all our requests is single-threaded, once the call goes to the callback, it actually goes the node.js codebase, which has 30% c++ native code. The V8 chrome engine then take care of using its own threads to process the operation
- use always async methods for performance benefits with C++ native code optimizing it
- There is an internal thread pool of worker threads default is 4 threads
- uses c++ async primitives where ever possible so that's why when you are downloading an image and if CPU is waiting for a request to finish, even in event loop this should not be a problem
- e.g kernel udp servers and clients
- two types so APIs node js has
- 1. synchronous
- 2. asynchronous (also called as fire and forget)
4. Design Patterns used in node js
- core patterns - like singleton, factory, builder pattern,
- control flow pattern - promise, async-await
- module design patter - dependency injection, revealing module pattern
- structural design patterns - proxy, adapter, decorator, composites
- behavior patterns - strategy, command, middleware
- messaging - request/reply, publish/subscribe
4. what is callback hell
- callback pattern is the default pattern manging outcome of an async method, but when nested results in unmanageable and unreadable code
- promises can use to solve it with .then() after .then()
- observables also can be used to solve it
5. Observable vs promise and callback hell
- Callbacks, Promises, and-await, rxjs
- Promises - using sequence of then's for callback hell
- RxJs Observable - being able to handle with streams with data, returns a collection as when producer get the result, the observer is ends
- async/await - uses promises behind the scenes, it gives a different way to handling promises once they resolve
6. Streams in Node.js
7. How to monitor the performance of an applicatioin
8. What would be the node.js code layout structure you would like
9. How will you secure configuration files
10. Hoisting
- Hoisting
- An important difference between function declarations and class declarations is that function declarations are hoisted and class declarations are not. You first need to declare your class and then access it
Comments
Post a Comment