Serverless route handler
NEXT JS allows applications to be full stack which means running the application both on the front end and on the back end
using the same file based routing system NEXT JS allows us to handle HTTP requests and develop back-end functionality without requiring an external server
Alternative to Express.js
users NEXT JS covers all of the features found in traditional backend servers which includes:
middleware parsing
authentication checks
serverless functions that simplify the deployment
scaling of API routes
3 Ways for defining route handler
File based route handlers in
app/api
folderdirect route handler in
app
directory using route.tsxSide Effect: NEXT JS won't know if it is a regular frontend route or backend route.
This means cannot have page.tsx and route.tsx on same folder.
Preventing routing in API Endpoint(backend) and pages in frontend
Server Actions
route.js is also a special file name and just for that reason it allows us to create those backend routes
Let's compare with Express
incredibly straightforward you simply need to write a get function and begin implementing your backend logic within it export async function get and then you return the response as if you're returning it from the regular Express server in the same way and in the same file you can use all the other HTTP verbs
EXPRESS.JS
NEXT JS
No need to setup any extra express configuration, making it convenient to code.
http://localhost:3000/api/users