Node 1
Node 1
o Answer: ES5 (ECMAScript 5) is the version of JavaScript that introduced features like
strict mode and JSON support. ES6 (ECMAScript 2015) introduced significant
enhancements such as arrow functions, let and const for variable declarations, template
literals, and classes. ES6 improves code readability and maintainability, making it easier
to write modern JavaScript applications. For building a Node.js server, ES6 can enhance
development efficiency and clarity.
2. Question: Write a code snippet to create a basic Express.js server that listens on port 4000 and
responds with "Welcome to My Server!" when the root endpoint (/) is accessed.
o Answer:
javascript
Copy code
res.send('Welcome to My Server!');
});
app.listen(PORT, () => {
});
3. Question: What are the purposes of the body-parser and cors middleware in an Express.js
application? Provide an example of how to use them.
javascript
Copy code
import bodyParser from 'body-parser';
app.use(cors());
app.use(bodyParser.json());
4. Question: Describe how you would set up a MySQL database connection in a Node.js
application. What parameters would you need to provide?
o Answer: To set up a MySQL connection, you would use a library like mysql2 and provide
parameters such as host, user, password, and database. Example:
javascript
Copy code
const db = mysql.createConnection({
host: 'localhost',
user: 'root',
password: 'yourpassword',
database: 'mydatabase'
});
5. Question: Define RESTful APIs. List and explain the four primary HTTP methods used in RESTful
services.
o Answer: RESTful APIs (Representational State Transfer) are web services that follow REST
principles, enabling interaction between client and server via HTTP. The four primary
HTTP methods are:
6. Question: Write a RESTful API endpoint in Express.js to create a new item in a MySQL database.
Include error handling in your code.
o Answer:
javascript
Copy code
db.query('INSERT INTO items (name) VALUES (?)', [name], (err, results) => {
if (err) {
});
});
7. Question: How can you implement input validation in an Express.js application? Provide an
example of a validation library you might use.
o Answer: Input validation can be implemented using libraries like express-validator or Joi.
For example, using express-validator:
javascript
Copy code
app.post('/items', [
body('name').isString().notEmpty()
if (!errors.isEmpty()) {
});
8. Question: Explain what middleware is in the context of Express.js. Give two examples of
middleware you could implement in a Node.js application.
o Answer: Middleware in Express.js is a function that has access to the request and
response objects and can modify them or end the request-response cycle. Examples
include:
Error Handling Middleware: Catches and processes errors that occur during
request processing.
9. Question: What is the significance of using appropriate HTTP status codes in API responses?
Provide examples of three status codes and their meanings.
o Answer: Appropriate HTTP status codes communicate the outcome of a request to the
client, indicating success or failure. Examples:
10. Question: Suppose you want to log all incoming requests to your Express server. Write a
middleware function that logs the HTTP method and the URL of each request.
o Answer:
javascript
Copy code
console.log(`${req.method} ${req.url}`);
next();
});
These questions and answers provide a comprehensive assessment of students' understanding of key
concepts related to Node.js, Express.js, and RESTful API development.
4o mini
2. Question: Describe how to create a simple Express.js server. What are the basic steps?
4. Set up a basic server that listens on a specified port and responds to a request.
o Answer: Middleware functions in Express.js are functions that have access to the
request and response objects. They can modify the request, end the request-response
cycle, or call the next middleware function. For example, a logging middleware might log
request details:
javascript
Copy code
console.log(`${req.method} ${req.url}`);
next();
});
4. Question: How do you set up a MySQL database connection in a Node.js application? What
parameters are required?
o Answer: To set up a MySQL database connection in Node.js, you can use the mysql2
package. Required parameters typically include host, user, password, and database.
Example:
javascript
Copy code
const db = mysql.createConnection({
host: 'localhost',
user: 'root',
password: 'yourpassword',
database: 'mydatabase'
});
o Answer: RESTful APIs (Representational State Transfer) are web services that follow the
principles of REST architecture. They allow clients to interact with server resources using
standard HTTP methods (GET, POST, PUT, DELETE). RESTful APIs are important because
they provide a stateless, scalable, and uniform interface for communication, making it
easier to build and maintain applications.
6. Question: Write a code snippet to create a RESTful API endpoint in Express.js that retrieves all
items from a MySQL database.
o Answer:
javascript
Copy code
if (err) {
res.json(results);
});
});
7. Question: What are HTTP status codes, and why are they important in API development?
o Answer: HTTP status codes are standardized codes returned by a server to indicate the
result of an HTTP request. They are important in API development because they inform
the client about the success or failure of a request, helping clients understand how to
proceed based on the response. For example, a 200 status indicates success, while a 404
status indicates that a resource was not found.
8. Question: How can you handle errors in an Express.js application? Provide an example of an
error handling middleware.
o Answer: You can handle errors in Express.js by defining an error handling middleware.
This middleware captures any errors that occur during request processing. Example:
javascript
Copy code
console.error(err.stack);
});
9. Question: What is input validation, and how can it be implemented in an Express.js application?
o Answer: Input validation is the process of checking incoming data to ensure it meets
certain criteria before processing it. It can be implemented in Express.js using libraries
like express-validator or Joi. For example, with express-validator:
javascript
Copy code
app.post('/items', [
body('name').isString().notEmpty()
if (!errors.isEmpty()) {
});
10. Question: Explain the significance of using the CORS middleware in an Express.js application.
o Answer: CORS (Cross-Origin Resource Sharing) is a security feature that restricts web
applications running in one domain from making requests to resources in another
domain. In an Express.js application, using the CORS middleware allows you to specify
which domains can access your API, enabling cross-origin requests and enhancing
security. You can implement it easily by using the cors package:
javascript
Copy code
import cors from 'cors';
app.use(cors());
These questions and answers should provide a comprehensive assessment of students' understanding of
the key concepts related to Node.js, Express.js, and RESTful API development.