You are building a chat application in Node.js and need to handle different types of messages like text, images, and files. How would you structure the event emitters and listeners to handle different message types efficiently?
- Create separate events for each message type: event.emit('textMessage', message)
- Use a single event and send message type as a parameter: event.emit('message', messageType, message)
- Create a listener for each message type: event.on('textMessage', textMessageCallback)
- Create a single listener and use conditional checks to handle message types: event.on('message', messageCallback)
To efficiently handle different message types in a chat application, it's best to create separate events for each message type (e.g., 'textMessage', 'imageMessage', 'fileMessage'). This approach keeps the code organized and allows specific handlers for each message type. The other options may lead to less structured and harder-to-maintain code.
What is the main purpose of the global object in Node.js?
- To manage global variables
- To handle exceptions globally
- To control the execution flow of the program
- To define local variables
The main purpose of the global object in Node.js is to manage global variables and provide a context for globally accessible methods and properties. It is not primarily responsible for handling exceptions, controlling execution flow, or defining local variables.
In Express.js, how does the order in which routes are defined affect the routing mechanism?
- Routes are matched in a random order
- Routes are matched in the order they are defined
- Routes are matched in reverse order
- Routes are matched based on alphabetical order
In Express.js, the order in which routes are defined affects the routing mechanism. Routes are matched in the order they are defined, and the first matching route is executed. This means that the order of route definitions is crucial as it determines which route will handle a request.
You are developing an NPM package and are about to publish a version with experimental features. What is the semantic versioning compliant way to version this release?
- 0.0.1
- 0.1.0
- 1.0.0
- 2.0.0
According to SemVer, when you have experimental or initial releases that may have breaking changes, you should start with version 0.0.1. This signifies that the package is in the early development stage.
How does parameterized query help in preventing SQL Injection attacks?
- It escapes special characters in user input
- It encrypts SQL queries before execution
- It restricts SQL queries to a predefined set
- It validates SQL queries against a whitelist
A parameterized query helps prevent SQL Injection attacks by escaping special characters in user input. This ensures that user input is treated as data and not executable code. The other options do not accurately describe how parameterized queries work against SQL Injection.
In Express, which method is used to start a server listening for connections?
- app.listen()
- server.start()
- express.startServer()
- node.start()
In Express, you use the app.listen() method to start a server that listens for incoming connections. This method binds the server to a specified port and hostname, allowing it to handle HTTP requests. The other options are not valid methods for starting an Express server.
How can you ensure that a specific script in package.json runs only after another specified script has successfully completed?
- Using npm's pre and post scripts
- Using conditional statements in JavaScript
- It's not possible to achieve this in package.json
- By using a third-party package
In package.json, you can use pre and post scripts to define the order in which scripts should run. For example, if you want a script to run after another, you can use "pre": "npm run firstScript" and "post": "npm run secondScript". This ensures that secondScript runs after firstScript.
In Express.js, what does the next() function do in middleware?
- Ends the request-response cycle
- Sends an HTTP response
- Passes control to the next middleware function
- Logs a message to the console
In Express.js, the next() function is used in middleware to pass control to the next middleware function in the stack. It allows the request to continue processing through subsequent middleware functions. If you don't call next(), the request-response cycle may be terminated, and subsequent middleware or route handlers won't be executed.
Which of the following is a common technique used for implementing full-text search in databases?
- Regular Expressions
- Binary Search
- Inverted Index
- Depth-First Search
A common technique for implementing full-text search in databases is the use of an inverted index. This index stores a list of words or terms along with the locations where they appear in the documents, making it efficient for searching large volumes of text. Regular expressions, binary search, and depth-first search are not typically used for full-text search in databases.
How does the use of subqueries impact the performance of SQL queries, and how can it be optimized?
- Subqueries can slow down SQL queries significantly. They execute for each row in the outer query result and may lead to performance bottlenecks. Optimization techniques include using JOINs, EXISTS, or rewriting queries as JOINs, ensuring indexes on relevant columns, and limiting the result set size.
- Subqueries have no impact on SQL query performance.
- Subqueries always improve SQL query performance.
- Subqueries should be avoided, but if used, no optimization is possible.
Subqueries can impact SQL query performance negatively if not used carefully. They can lead to increased query execution times, and optimizing them involves using appropriate techniques to minimize their impact.