Basics of Node.js:
What is Node.js?
- Node.js is a JavaScript runtime built on Chrome's V8 JavaScript engine that allows developers to execute JavaScript code on the server side.
Explain the event-driven architecture of Node.js.
- Node.js follows an event-driven, non-blocking I/O model. It uses an event loop to handle asynchronous operations, allowing efficient processing of multiple requests.
What is the Node Package Manager (NPM)?
- NPM is the default package manager for Node.js. It is used to install, manage, and share packages and dependencies for Node.js applications.
How does Node.js handle concurrency?
- Node.js is single-threaded, but it uses an event-driven, non-blocking model to handle concurrency. Asynchronous operations, such as I/O operations, are performed using callbacks or Promises.
Explain the role of the V8 JavaScript engine in Node.js.
- V8 is an open-source JavaScript engine developed by Google. Node.js uses V8 to execute JavaScript code efficiently on the server side.
Modules and CommonJS:
What is a module in Node.js?
- A module in Node.js is a reusable piece of code encapsulated in a file. It can include variables, functions, and classes that can be exported and used in other modules.
How do you include a module in Node.js?
- Modules are included using the
require
function. For example:const myModule = require('./myModule');
- Modules are included using the
What is the purpose of the
module.exports
object?- The
module.exports
object is used to expose functions, objects, or variables from a module, making them accessible to other parts of the application.
- The
Explain the CommonJS module format.
- CommonJS is a module system for JavaScript used by Node.js. It specifies a simple format for modules, including the use of
require
andmodule.exports
.
- CommonJS is a module system for JavaScript used by Node.js. It specifies a simple format for modules, including the use of
How do you create a custom event in Node.js?
- The
events
module in Node.js is used to create and handle custom events. You can create an instance of theEventEmitter
class and use its methods to emit and listen for events.
- The
Asynchronous Programming:
What is callback hell? How can it be avoided?
- Callback hell refers to the situation where multiple nested callbacks make the code difficult to read and maintain. It can be avoided by using Promises or async/await.
Explain the concept of Promises in Node.js.
- Promises in Node.js are a way to handle asynchronous operations. They represent a value that may be available now, in the future, or never, resolving or rejecting with a value.
What is the purpose of the
async
andawait
keywords in Node.js?async
andawait
are used to work with asynchronous code more comfortably. Theasync
keyword is used to define asynchronous functions, andawait
is used to wait for a promise to resolve.
How does the
setImmediate
function work in Node.js?setImmediate
is used to execute a callback function in the next iteration of the event loop. It is similar tosetTimeout(fn, 0)
but with some differences in execution order.
What is the purpose of the
process.nextTick
function?process.nextTick
is used to schedule a callback function to be executed in the next iteration of the event loop, immediately after the current operation.
Express.js:
What is Express.js?
- Express.js is a web application framework for Node.js that simplifies the development of web applications by providing a robust set of features for routing, middleware, and handling HTTP requests and responses.
Explain the middleware concept in Express.js.
- Middleware in Express.js are functions that have access to the request, response, and the next middleware function in the application’s request-response cycle. They can modify the request and response objects and terminate the request-response cycle.
How do you handle routing in Express.js?
- Routing in Express.js is handled using the
express.Router
middleware. It allows developers to define routes and their corresponding handlers separately and then mount them on the main application.
- Routing in Express.js is handled using the
What is the purpose of the
app.use
function in Express.js?app.use
is used to mount middleware functions in the Express.js application. It is called for every incoming request and can be used for tasks such as logging, authentication, and error handling.
Explain the difference between
app.get()
andapp.post()
in Express.js.app.get()
is used to handle HTTP GET requests, whileapp.post()
is used to handle HTTP POST requests. They correspond to the HTTP methods they represent.
RESTful APIs and HTTP:
What is RESTful architecture?
- REST (Representational State Transfer) is an architectural style that defines a set of constraints for designing web services. RESTful APIs use standard HTTP methods (GET, POST, PUT, DELETE) to perform CRUD operations.
How do you handle query parameters in Express.js?
- Query parameters in Express.js can be accessed using
req.query
. For example,req.query.paramName
retrieves the value of the parameter namedparamName
.
- Query parameters in Express.js can be accessed using
What is the purpose of the HTTP status code 404?
- The HTTP status code 404 indicates that the requested resource could not be found on the server.
How can you secure Express.js applications?
- Express.js applications can be secured by implementing measures such as using HTTPS, validating user input, setting secure HTTP headers, and implementing authentication and authorization mechanisms.
Explain the concept of CORS in Express.js.
- CORS (Cross-Origin Resource Sharing) is a security feature implemented by browsers to restrict web pages from making requests to a different domain than the one that served the web page. In Express.js, CORS can be enabled using the
cors
middleware.
- CORS (Cross-Origin Resource Sharing) is a security feature implemented by browsers to restrict web pages from making requests to a different domain than the one that served the web page. In Express.js, CORS can be enabled using the
MongoDB and Mongoose:
What is MongoDB?
- MongoDB is a NoSQL database that stores data in JSON-like documents. It is designed for scalability, flexibility, and performance.
Explain the concept of collections in MongoDB.
- In MongoDB, a collection is a group of MongoDB documents. It is roughly equivalent to an RDBMS table.
What is Mongoose in the context of Node.js?
- Mongoose is an ODM (Object-Document Mapper) library for MongoDB and Node.js. It provides a schema-based solution for modeling application data and interacting with MongoDB.
How do you perform CRUD operations in MongoDB using Mongoose?
- Mongoose provides methods like
save
,find
,update
, andremove
to perform CRUD operations on MongoDB documents.
- Mongoose provides methods like
What is the purpose of the Mongoose schema in Node.js?
- Mongoose schema defines the structure of documents in a MongoDB collection. It specifies the fields, types, and constraints for the documents.
Authentication and Authorization:
How can you implement user authentication in Node.js? - User authentication in Node.js can be implemented using various strategies such as username/password, JWT (JSON Web Tokens), OAuth, and third-party authentication providers.
Explain the purpose of JWT (JSON Web Tokens) in Node.js authentication.
- JWT is a compact, URL-safe means of representing claims to be transferred between two parties. In the context of Node.js authentication, JWTs are commonly used to encode user information and verify the authenticity of requests.
What is OAuth, and how does it relate to Node.js authentication?
- OAuth is an open standard for access delegation, commonly used as a way for Internet users to grant websites or applications access to their information on other websites without giving them their credentials. In Node.js, OAuth can be used for third-party authentication.
How do you handle user sessions in Express.js?
- User sessions in Express.js can be handled using middleware like
express-session
. Sessions are often used to store user authentication status and other user-related information.
- User sessions in Express.js can be handled using middleware like
Explain the concept of role-based access control in Node.js.
- Role-based access control is a method of regulating access to a system based on roles assigned to users. In Node.js, roles can be used to determine the permissions and access levels of users.
WebSocket and Real-time Communication:
What is WebSocket, and how is it different from HTTP?
- WebSocket is a communication protocol that provides full-duplex communication channels over a single, long-lived connection. It is different from HTTP, which follows a request-response model.
How can you implement WebSocket in Node.js?
- WebSocket can be implemented in Node.js using libraries like
ws
or the WebSocket support provided by frameworks like Express.js.
- WebSocket can be implemented in Node.js using libraries like
Explain the concept of real-time communication in Node.js.
- Real-time communication in Node.js involves bidirectional communication between clients and servers, allowing instant data updates without the need for manual refreshing.
Testing in Node.js:
What are unit tests, and how can they be implemented in Node.js?
- Unit tests are tests that focus on a specific unit of code, such as a function or module, to ensure that it behaves as expected. Unit tests in Node.js can be implemented using testing frameworks like Mocha or Jest.
What is Mocha, and how does it facilitate testing in Node.js?
- Mocha is a feature-rich JavaScript test framework for Node.js and browsers. It provides support for asynchronous testing, before/after hooks, and various reporting options.
Deployment and DevOps:
How can you deploy a Node.js application?
- Node.js applications can be deployed using various platforms, cloud services, and deployment tools. Popular choices include AWS, Heroku, and Docker.
What is PM2, and how is it used in Node.js deployment?
- PM2 is a process manager for Node.js applications. It ensures that applications are always running and provides features like automatic restarts, load balancing, and monitoring.
Explain the concept of continuous integration in Node.js development.
- Continuous Integration (CI) is a software development practice where changes to the codebase are automatically tested and integrated into the main code repository. Tools like Jenkins, Travis CI, and GitHub Actions can be used for CI in Node.js projects.
How do you handle environment variables in Node.js?
- Environment variables in Node.js can be handled using the
process.env
object. Libraries likedotenv
can be used to manage and load environment variables from a configuration file.
- Environment variables in Node.js can be handled using the
Security in Node.js:
What are some common security vulnerabilities in Node.js applications?
- Common security vulnerabilities in Node.js applications include injection attacks, XSS (Cross-Site Scripting), CSRF (Cross-Site Request Forgery), and insecure dependencies.
How can you prevent SQL injection in Node.js applications?
- SQL injection can be prevented by using parameterized queries or prepared statements when interacting with databases. Libraries like
knex
provide mechanisms for safe query construction.
- SQL injection can be prevented by using parameterized queries or prepared statements when interacting with databases. Libraries like
Explain the concept of Cross-Origin Resource Sharing (CORS) in Node.js.
- CORS is a security feature implemented by browsers to restrict web pages from making requests to a different domain than the one that served the web page. In Node.js, CORS can be handled using middleware or headers.
How can you secure file uploads in Node.js?
- File uploads in Node.js can be secured by validating file types, setting file size limits, and storing files in a secure location. Libraries like
multer
can be used to handle file uploads.
- File uploads in Node.js can be secured by validating file types, setting file size limits, and storing files in a secure location. Libraries like
Miscellaneous:
What is the purpose of the
child_process
module in Node.js?- The
child_process
module in Node.js is used to spawn child processes, allowing the execution of external commands or scripts from within a Node.js application.
- The
How do you handle errors in Node.js?
- Errors in Node.js can be handled using try-catch blocks for synchronous code and using
.catch()
ortry
/catch
for asynchronous code. Additionally, middleware likeexpress-error-handler
can be used to handle errors in Express.js applications.
- Errors in Node.js can be handled using try-catch blocks for synchronous code and using