Jump into concepts and practice - no test required
or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Creating Documents with Express
📖 Scenario: You are building a simple web server that can create and serve text documents on request. This is useful for generating reports, notes, or any text content dynamically.
🎯 Goal: Build an Express server that creates a text document from given data and serves it to the user when they visit a specific URL.
📋 What You'll Learn
Create an Express app instance
Set up a route to handle GET requests at /document
Generate a text document content dynamically
Send the generated document as a downloadable file to the client
💡 Why This Matters
🌍 Real World
Web servers often need to generate and send files like reports, invoices, or logs dynamically to users.
💼 Career
Knowing how to create and serve files with Express is a common task for backend developers working with Node.js.
Progress0 / 4 steps
1
Set up Express app
Create a variable called express by requiring the 'express' module. Then create an Express app instance called app by calling express().
Express
Hint
Use require('express') to import Express and then call it as a function to create the app.
2
Create document content variable
Create a variable called documentContent and assign it the string 'This is a sample document created with Express.'.
Express
Hint
Assign the exact string to documentContent.
3
Add route to serve document
Use app.get to create a route for '/document'. Inside the route handler, use res.setHeader to set 'Content-Disposition' to 'attachment; filename="sample.txt"' and 'Content-Type' to 'text/plain'. Then send documentContent as the response using res.send(documentContent).
Express
Hint
Use app.get with '/document' and set headers before sending the content.
4
Start the Express server
Use app.listen to start the server on port 3000. Provide a callback function that logs 'Server running on port 3000' to the console.
Express
Hint
Use app.listen(3000, () => { console.log(...) }) to start the server.
Practice
(1/5)
1. Which Express method is used to handle creating new documents via HTTP POST requests?
easy
A. app.post()
B. app.get()
C. app.put()
D. app.delete()
Solution
Step 1: Understand HTTP methods in Express
Express uses different methods like get, post, put, and delete to handle HTTP requests.
Step 2: Identify method for creating new data
The post method is used to create new documents or data entries.
Final Answer:
app.post() -> Option A
Quick Check:
Creating documents = app.post() [OK]
Hint: Use app.post() for creating new data entries [OK]
Common Mistakes:
Using app.get() instead of app.post()
Confusing app.put() with app.post()
Using app.delete() for creation
2. Which code snippet correctly enables JSON parsing middleware in Express to access req.body?
easy
A. app.use(express.urlencoded());
B. app.use(express.json());
C. app.use(bodyParser.text());
D. app.use(express.static('public'));
Solution
Step 1: Identify middleware for JSON parsing
Express provides express.json() middleware to parse JSON request bodies.
Step 2: Match correct usage
The correct way is app.use(express.json()); to enable JSON parsing.
Final Answer:
app.use(express.json()); -> Option B
Quick Check:
JSON parsing middleware = express.json() [OK]
Hint: Use app.use(express.json()) to read JSON body [OK]
Common Mistakes:
Using express.urlencoded() for JSON data
Using bodyParser.text() instead of JSON parser
Forgetting to enable any parser middleware
3. What will be the HTTP status code sent when the following Express route successfully creates a document?
Hint: Always enable express.json() before accessing req.body [OK]
Common Mistakes:
Thinking res.send must be res.json
Using app.get for creation routes
Assuming saveUser must be async here
5. You want to create an Express route that accepts JSON data to create a new product and respond with the created product including an ID. Which code snippet correctly implements this?
Step 1: Use correct HTTP method and access JSON body
The route uses app.post and accesses req.body which is correct for creating a product.
Step 2: Add an ID and respond with status 201 and JSON
Assigning product.id = Date.now() simulates creating an ID. Responding with res.status(201).json(product) sends the created product with proper status.