Font size
WorksheetsExpress.js and REST Worksheet — Grade 13
Total questions: 66
Worksheet time: 33mins
Which HTTP method is used to update an existing resource?
(a)
Chained route handlers for the same route path in Express can be created by using
(a)
What is the main goal of middleware in Express?
(a)
Which command creates a package.json file with default values?
(a)
How to define a dynamic route in Express?
(a)
Part 1 — Multiple Correct Answers: What statements are correct about the fs module?
Can read files asynchronously
Can write files to disk
Part 1 — Multiple Correct Answers: What are the REST architectural constraints?
Uniform interface
Code on demand
Statelessness
Part 1 — Multiple Correct Answers: Which of the following are types of middleware in Express?
Application-level middlewares
Error-handling middlewares
Router-level middlewares
Part 1 — Multiple Correct Answers: Which statements about Express Router are correct?
Modularizes routes
Can be mounted using app.use()
Supports middleware
Which are valid use cases for a middleware in Express?
(a)
Part 2 — RESTful API Question: In a RESTful API for a Library system, which URL/Method combinations make sense?
GET /books (To see all books)
DELETE /books/:id (To remove a specific book)
POST /books (To create a new book)
PUT /books (To delete all books) — incorrect; PUT is for updating a resource, not deleting all books
Part 3 — Short Code Questions: Write the command to install the nodemon package as a global package.
(a)
Part 3 — Short Code Questions: Write the line of code to export a function named add from a file using CommonJS syntax.
(a)
Part 3 — Short Code Questions: How do you access a dynamic URL parameter with the name “id” inside an Express route?
(a)
Part 3 — Short Code Questions: Write the line of code to serve a static folder named “public” using Express.
(a)
Part 3 — Short Code Questions: Write an express method chaining an HTTP status code for Not Found with a response body.
(a)
Part 4 — Fill in Blanks: Middleware that parses JSON payloads called
(a)
Part 4 — Fill in Blanks: HTTP Method to modify the resource is
(a)
Part 4 — Fill in Blanks: Identifier for a resource (e.g., /api/users).
(a)
HTTP Status code for successful creation is
(a)
Built-in Node.js module for creating a server.
(a)
Part 3 (другой вариант) — Short One-Line Code Questions: Write code to define a POST route for /api/products.
(a)
Part 3 (другой вариант) — Short One-Line Code Questions: Write code to define a middleware that logs the request method and URL for an Express router.
(a)
Part 3 (другой вариант) — Short One-Line Code Questions: How do you access a query parameter named sort inside an Express route?
(a)
Part 3 (другой вариант) — Short One-Line Code Questions: Write code to parse JSON data in Express using middleware.
(a)
Part 3 (другой вариант) — Short One-Line Code Questions: Define a GET route /about that sends back the about.html file.
(a)
Part 4 (другой вариант) — HTTP Status Codes: A client successfully sends a POST request to /api/products, and the product is created in the database.
(a)
Part 4 (другой вариант) — HTTP Status Codes: A client sends a GET request to /api/profile without being authenticated, and authentication middleware blocks the request.
(a)
A client sends a DELETE request to /api/orders/55, and the order is successfully removed. Which HTTP status code should the server return?
204 No Content
200 OK
404 Not Found
503 Service Unavailable
A client sends a GET request to /api/cart, but the route does not exist in the Express application. Which HTTP status code should the server return?
404 Not Found
400 Bad Request
301 Moved Permanently
503 Service Unavailable
A client sends a request to an Express server that is temporarily down for maintenance. Which HTTP status code should the server return?
503 Service Unavailable
500 Internal Server Error
404 Not Found
302 Found
Which engine does Node.js use to execute JavaScript?
SpiderMonkey
Chrome V8
Java HotSpot
Nashorn
In the terminal, what happens when you press Ctrl + C (cmd + C)?
Clears the screen
Stops the execution
Opens a new shell
Pauses the process without terminating
What is the use of dotenv in Node.js applications?
Compile TypeScript to JavaScript
Manage environmental variables
Handle HTTP routing
Run unit tests
Which of the following is NOT true about REST APIs?
REST APIs must use JSON exclusively.
REST can use different representations like XML, YAML, or plain text.
REST is based on stateless communication.
Resources are identified by URLs and manipulated via standard HTTP methods.
Which Node.js core module is used to read and write files?
http
fs
path
url
In an Express route, which object contains route variables like the user id in /users/123?
req.query
req.params
req.body
res.locals
Which of the following are differences between the Browser and Node.js environments? Select all that apply.
Browsers have the window object; Node.js has global.
Node.js can access the local filesystem; Browsers generally cannot.
Sub-environments have access to the document object (DOM).
Node.js allows you to choose your version; Browsers are controlled by the user.
What information is typically contained in an HTTP Request? Select all that apply.
HTTP Method
Request Headers
The Server’s RAM capacity
Request Body
What are the characteristics of the Node.js Event Loop? Select all that apply.
It’s single-threaded.
Performs blocking I/O by default.
Offloads tasks to the system kernel when possible.
Allows for non-blocking I/O operations.
Which of these are valid built-in Express middleware? Select all that apply.
express.json()
express.static()
express.routes()
express.urlencoded()
Which features are provided by the Multer middleware? Select all that apply.
Handling multipart/form-data used for file uploads
Sending emails with attachments
Validating uploaded files’ size and type
Automatically saving files to disk or memory
Which npm commands or files help ensure reproducible builds? Select all that apply.
package-lock.json
node_modules folder
npm update
npm install
What is correct about Node.js EventEmitter class? Select all that apply.
Every object in Node.js inherits from EventEmitter.
Listeners can be added using on or once methods.
Allows you to create, emit, and listen for custom events.
It’s used to handle HTTP requests.
Which of the following are Node.js global objects or classes? Select all that apply.
Buffer
console
process
env
What is right about the events core module? Select all that apply.
Provides the EventEmitter class
Allows asynchronous event-driven programming
Automatically manages HTTP sessions
Not built-in and needs installation
Which statements about middleware in Express are correct? Select all that apply.
Can modify the request and response objects
Must always send a response
Can be chained using next()
Executes after the response is sent
Choose the correct Express route that extracts the id parameter from /users/:id and responds with JSON { userId:
app.get('/users/:id', (req, res) => { res.json({ userId: req.params.id }); });
app.get('/users', (req, res) => { res.json({ userId: req.query.id }); });
app.post('/users/:id', (req, res) => { res.send(req.params.id); });
app.get('/users/:id', (req, res) => { res.send({ id: req.body.id }); });
Choose the correct GET route that responds from /api/users with JSON { users: [] }.
app.get('/api/users', (req, res) => { res.json({ users: [] }); });
app.post('/api/users', (req, res) => { res.json({ users: [] }); });
app.get('/api/users', (req, res) => { res.send([]); });
app.get('/users', (req, res) => { res.json({ users: [] }); });
Choose the middleware that logs the HTTP method and URL for each incoming request.
app.use((req, res, next) => { console.log(` req.method {req.url}`); next(); });
app.use((req, res) => { console.log(req.body); });
app.get('*', (req, res) => { console.log(req.method); });
app.use((req, res, next) => { next(); console.log(` req.method {req.url}`); });
Declare a permanent redirection from old route /profile to new route /dashboard in Express. Select the snippet that performs a 301 redirect from /profile to /dashboard.
js app.get('/profile', (req, res) => { res.redirect(301, '/dashboard'); });
js app.get('/dashboard', (req, res) => { res.redirect('/profile'); });
js app.use('/profile', (req, res) => { res.redirect(302, '/dashboard'); });
js app.get('/profile', (req, res) => { res.redirect('/dashboard'); });
Write an Express error-handling middleware that logs the error and sends a response with “Internal Server Error”. Choose the correct middleware signature and response.
js app.use((err, req, res, next) => { console.error(err); res.status(500).send('Internal Server Error'); });
js app.use((req, res, next) => { console.error('error'); res.status(500).json({ error: 'Internal Server Error' }); });
js app.get((err, req, res, next) => { console.log(err); res.sendStatus(404); });
js app.use((err, req, res) => { res.status(500).send(err); });
Write code to start an Express server on port 5000. Select the snippet that listens on port 5000 and logs a startup message.
js app.listen(5000, () => { console.log('Server running on port 5000'); });
js app.start(5000, () => { console.log('Server started'); });
js app.listen(process.env.PORT, () => { console.log('Server running'); });
js app.listen(3000); console.log('Server running on port 5000');
Write a command to run a script named test from package.json.
(a)
Write code to read an environment variable named PORT.
(a)
Write one line to parse URL-encoded form data in Express.
(a)
You want to design an API endpoint that retrieves data from the server without modifying anything. Fill in the HTTP method.
(a)
A client sends data to the server to create a new resource. Fill in the HTTP method.
(a)
If a client wants to remove a resource identified by its ID. Fill in the HTTP method.
(a)
A client needs to modify only certain fields of a resource without replacing the entire resource. Fill in the HTTP method.
(a)
When a client wants to replace an entire existing resource with new data. Fill in the HTTP method.
(a)
A valid request is sent to the server, but the database connection fails unexpectedly during processing. Fill in the appropriate HTTP status.
(a)
A client sends a PUT request to /api/users but the API only supports GET and POST for this endpoint. Fill in the appropriate HTTP status.
(a)
A client sends a GET request to /api/orders/101 but no order with that ID exists in the database. Fill in the appropriate HTTP status.
(a)
A logged-in user attempts to access DELETE /api/users/7 but doesn't have administrator privileges. Fill in the appropriate HTTP status.
(a)
A client sends a POST request to /login without a password field. The server cannot process the request due to missing required data. Fill in the appropriate HTTP status.
(a)
