Basics of Node.js:
-
What is Node.js?
- Node.js is a server-side JavaScript runtime environment built on the V8 JavaScript engine.
-
How does Node.js differ from traditional JavaScript?
- Node.js is not a language; it allows JavaScript to be executed server-side, outside the browser.
-
Explain the event-driven architecture of Node.js.
- Node.js operates on a non-blocking, event-driven model where events trigger asynchronous callbacks.
-
What is an EventEmitter in Node.js?
- EventEmitter is a class in Node.js that allows objects to emit and listen for events.
Node.js Modules:
-
What is a module in Node.js?
- A module is a reusable piece of code that encapsulates functionality.
-
How can you include external libraries in Node.js?
- Use
require('module')
to include external libraries.
- Use
npm (Node Package Manager):
-
What is npm?
- npm is the Node Package Manager used for package management and distribution.
-
How do you install packages using npm?
- Use
npm install package-name
to install packages locally andnpm install -g package-name
for global installation.
- Use
-
Explain the purpose of
package.json
.package.json
is a file containing metadata about a Node.js project, including dependencies.
Callbacks and Promises:
-
What is a callback function in Node.js?
- A callback is a function passed as an argument to another function, to be executed later.
-
Explain the concept of callback hell.
- Callback hell refers to the nesting of multiple callbacks, making the code hard to read and maintain.
-
What are Promises in Node.js?
- Promises are objects representing the eventual completion or failure of an asynchronous operation.
-
How do you handle errors in Promises?
- Use
.catch()
or.then(null, errorHandler)
to handle errors in Promises.
- Use
File System:
-
How do you read and write files in Node.js?
- Use
fs.readFile
for reading andfs.writeFile
for writing files.
- Use
-
Explain the difference between
fs.readFileSync
andfs.readFile
.fs.readFileSync
is synchronous, blocking execution, whilefs.readFile
is asynchronous.
-
What is the purpose of the
fs.createReadStream
method?- It creates a readable stream to efficiently read large files.
Express.js:
-
What is Express.js?
- Express.js is a web application framework for Node.js, simplifying the process of building robust web applications.
-
How do you install Express.js?
- Use
npm install express
to install Express.js.
- Use
-
Explain the routing in Express.js.
- Routing in Express.js defines how the application responds to client requests.
-
What is middleware in Express.js?
- Middleware functions are functions that have access to the request, response, and the next middleware function in the application’s request-response cycle.
RESTful APIs:
-
What is RESTful architecture?
- RESTful architecture is a style of designing networked applications using simple HTTP methods.
-
How do you create a RESTful API using Express.js?
- Use Express.js to define routes and handlers for HTTP methods.
-
Explain the HTTP methods used in RESTful services.
- GET (read), POST (create), PUT (update), DELETE (delete).
Asynchronous Programming:
-
What is the event loop in Node.js?
- The event loop is a core concept in Node.js for handling asynchronous operations.
-
How does Node.js handle asynchronous code?
- Through callbacks, Promises, and async/await.
-
What is the purpose of the
setImmediate
function?setImmediate
is used to execute a script once the current event loop cycle completes.
Streams:
-
What are streams in Node.js?
- Streams provide an efficient way to read or write data in chunks.
-
Explain the difference between readable and writable streams.
- Readable streams allow reading, while writable streams allow writing.
-
How do you pipe streams in Node.js?
- Use the
pipe
method to connect the output of one stream to the input of another.
- Use the
WebSocket:
-
What is WebSocket?
- WebSocket is a communication protocol providing full-duplex communication channels over a single TCP connection.
-
How do you implement WebSocket in Node.js?
- Use the
ws
library or thesocket.io
library for WebSocket implementation.
- Use the
MongoDB and Mongoose:
-
What is MongoDB?
- MongoDB is a NoSQL database.
-
How do you connect to a MongoDB database using Node.js?
- Use the
mongodb
driver or an ODM like Mongoose.
- Use the
-
What is Mongoose?
- Mongoose is an ODM (Object-Document Mapper) for MongoDB and Node.js.
-
Explain the schema in Mongoose.
- A schema defines the structure of documents in a collection.
Testing in Node.js:
-
What testing frameworks are commonly used in Node.js?
- Mocha, Jasmine, Jest.
-
How do you write unit tests in Node.js?
- Use testing frameworks like Mocha and assertions libraries like Chai.
Security:
- How can you prevent common security vulnerabilities in Node.js applications?
- Validate input, use parameterized queries, sanitize user inputs, and keep dependencies updated.
Debugging:
- How do you debug a Node.js application?
- Use the
debugger
statement or tools likenode-inspector
and built-in debugging in VSCode.
- Use the
Performance Optimization:
- What techniques can you use to optimize the performance of a Node.js application?
- Caching, load balancing, minimizing blocking code, and using a reverse proxy.
Child Processes:
- Explain the use of the
child_process
module in Node.js.- It allows running external processes, enabling interaction with the operating system.
Global Objects in Node.js:
-
What is the global object in Node.js?
- The
global
object represents the global namespace in Node.js.
- The
-
Explain the purpose of
__dirname
and__filename
.__dirname
is the name of the directory containing the currently executing script, and__filename
is the file name of the current module.
Error Handling:
- How do you handle errors in Node.js?
- Use try-catch blocks, callback error-first pattern, and Promise
.catch()
.
- Use try-catch blocks, callback error-first pattern, and Promise
Deployment:
- What are some common deployment strategies for Node.js applications?
- Using containers (Docker), cloud platforms (AWS, Azure, Heroku), and continuous integration.
Scalability:
- How can you scale a Node.js application?
- Horizontal scaling with load balancing, using a reverse proxy, and optimizing code.
Middleware:
- Explain the concept of middleware in Express.js.
- Middleware functions are functions that have access to the request, response, and the next middleware function in the application’s request-response cycle.
Cross-Origin Resource Sharing (CORS):
- What is CORS, and how can you handle it in Express.js?
- CORS (Cross-Origin Resource Sharing) is a security feature implemented by browsers. In Express.js, you can use the
cors
middleware to handle it.
- CORS (Cross-Origin Resource Sharing) is a security feature implemented by browsers. In Express.js, you can use the
Template Engines:
- What is a template engine, and which ones are commonly used with Node.js?
- A template engine processes templates to produce HTML. Common ones for Node.js are EJS, Handlebars, and Pug.
WebSockets:
- How do WebSockets differ from traditional HTTP communication?
- WebSockets provide full-duplex communication, allowing real-time data transfer, unlike the request-response nature of HTTP.
Comments