app.get(path, handler) listens for GET requests on the path.
Examples
Sends a welcome message when someone visits the home page.
Express
app.get('/', (req, res) => {
res.send('Welcome to my site!');
});
Creates a new page at /about with a simple text response.
Express
app.get('/about', (req, res) => {
res.send('About us page');
});
Starts the server on port 4000 instead of the default 3000.
Express
app.listen(4000, () => {
console.log('Server running on port 4000');
});
Sample Program
This program creates a simple Express server. When you visit http://localhost:3000/ in your browser, it shows "Hello World!". The console logs a message to confirm the server is running.
D. import express from 'express';
const app = express();
app.get('/data', (req, res) => {
res.json('Hello JSON');
});
app.listen(8080);
Solution
Step 1: Check route method and path
The question asks for a GET route at '/data', so app.get('/data', ...) is correct.
Step 2: Verify response and port
Response should be JSON, so res.json({ message: 'Hello JSON' }) is correct. The server must listen on port 8080.
Step 3: Eliminate alternatives with errors
Alternatives fail due to using POST instead of GET, wrong port like 3000, res.send('string') instead of res.json(object), or res.json('string') instead of object.
Final Answer:
The snippet with app.get('/data'), res.json({ message: 'Hello JSON' }), app.listen(8080) -> Option C
Quick Check:
GET /data + res.json + port 8080 [OK]
Hint: Use app.get + res.json + correct port [OK]
Common Mistakes:
Using POST instead of GET for route
Sending string instead of JSON object
Listening on wrong port number
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.