|
extJS is a React framework that is used to build full–stack web applications. It is used both for front-end as well and back–end. It comes with a powerful set of features to simplify the development of React applications. One of its features is Route Handler. In this article, we will learn about Route Handler with its syntax and examples. Route Handler:Route Handler is a feature that allows you to create custom request handlers for specific routes to handle different types of requests. route.js file is used to create a custom route handler within the app directory. By using this file you can create a API to handle web requests and responses. You can create a custom route handler for different HTTP methods such as GET, POST, PUT, PATCH, DELETE, HEAD, and OPTIONS. You can handle HTTP requests and responses by using NextRequest and NextResponse. Syntax for Different HTTP methods:import { NextResponse } from "next/server"; Steps to Create NextJS Application:Step 1: Create a NextJS application using the following command. npx create-next-app@latest gfg Step 2: It will ask you some questions, so choose as the following. √ Would you like to use TypeScript? ... No Step 3: After creating your project folder i.e. gfg, move to it using the following command. cd gfg Folder Structure:Example: The below example demonstrates the use of route.js (Route Handler). Note: Remove the included css file from layout.js file.
Start your application using the command. npm run dev Output: |
Reffered: https://www.geeksforgeeks.org
ReactJS |
Type: | Geek |
Category: | Coding |
Sub Category: | Tutorial |
Uploaded by: | Admin |
Views: | 14 |