How to make Apis in Next.js using route handlers
GET /api/user
GET /api/user/${id}
Parameter | Type | Description |
---|---|---|
id |
string |
Required. Id of user to fetch |
POST /api/user
PUT /api/user/id
Parameter | Type | Description |
---|---|---|
id |
string |
Required. Id of user to Update |
Delete /api/user/id
Parameter | Type | Description |
---|---|---|
id |
string |
Required. Id of user to Delete |
export async function DELETE(req:Request,context:any){
const id=context.params.id
await fetch(`${url}/${id}`,{
method:'DELETE',
headers:{
'Content-Type':"application/json"
}
})
return NextResponse.json({message:'user has been deleted'})
}
Clone the project
git clone https://github.com/imarjunshrma/next.js_crud_api.git
Go to the project directory
cd my-project
Install dependencies
npm install
Start the server
npm run dev
React,Node,Angular,React Native,Prisma,db,C,C++...
I'm a full stack developer...