Response vs NextResponse

Response is a constructor function of JavaScript that is used to create a response object. The object is used to send the data to the client. 

NextResponse is a constructor function of Next.js that is used to create a response object. The object is used to send the data to the client. 

Both response objects will follow almost the same syntax, but, they have some differences in the second parameter named options.


Response constructor:

const response = new Response(body, options);

const myOptions = { status: 200, statusText: "SuperSmashingGreat!" }; 

const myResponse = new Response(myBlob, myOptions);


NextResponse constructor:

import { NextResponse } from "next/server";

const response = new NextResponse(body, init);

return new NextResponse(JSON.stringify(posts), { headers: { "Content-Type": "application/json" },});

Post a Comment

Previous Post Next Post