Q14: What are API Routes in pages/api/ and how are they created?
Files inside pages/api/ map to HTTP API endpoints rather than HTML pages. They export a default request handler function function handler(req, res) that runs on Node.js server runtimes.
Basic API Route Handler
// pages/api/hello.js -> Maps to /api/hello
export default function handler(req, res) {
res.status(200).json({ message: 'Hello World' });
}💡 Default exported handler manages incoming requests to the /api/hello endpoint.
Key Takeaways
- Files in
pages/api/serve JSON/data responses instead of HTML UI. - Handler functions receive standard Node.js req and res objects.
Knowledge Check
Do files inside pages/api/ generate frontend React HTML component outputs?