Next.js API Routes
Next.js API Routes
Next.js has support for API Routes, which let you easily create an API endpoint as a Node.js serverless function.
Any file inside the pages/api
folder is mapped to /api/*
and will be treated as an API endpoint instead of a page
. They are server-side only bundles and won't increase your client-side bundle size.
You can create an API endpoint by creating a function inside the pages/api
directory in the following format:
// req = HTTP incoming message, res = HTTP server response
export default function handler(req, res) {
// ...
}
Note that:
req
is an instance of http.IncomingMessage, plus some pre-built middlewares you can see here.res
is an instance of http.ServerResponse, plus some helper functions you can see here.