TechDecode BlogTechDecode
  • Home
  • Posts

© 2026 TechDecode. All rights reserved.

TermsPrivacyContact
Technology
Tutorial
Development

Next.js API Routes Building Server-Side Functionality

Blog Owner
Blog Owner
July 20, 2025
2 min read
329 words
Next.js API Routes Building Server-Side Functionality
The pages/api/users.js file defines an API route accessible at /api/users. The handler function accepts two parameters: req (a NextApiRequest object representing the HTTP request) and res (a NextApiResponse object for the HTTP response). For GET requests, it returns a user list (hardcoded here, but typically queried from a database in practice). For POST requests, it accepts user data from the request body, simulates adding it to a database, and returns a success message. For unsupported methods, it returns a 405 Method Not Allowed error with allowed methods specified. Next.js API Routes enable the creation of standalone server-side functionality within a Next.js application, capable of handling HTTP requests and returning JSON data or other responses. API routes reside in the pages/api directory, with each file mapping to a specific API endpoint.

Basic Example

pages/api/users.js

import type { NextApiRequest, NextApiResponse } from 'next';

// Get user list
export default async function handler(req: NextApiRequest, res: NextApiResponse) {
  if (req.method === 'GET') {
    const users = [
      { id: 1, name: 'User 1' },
      { id: 2, name: 'User 2' },
      { id: 3, name: 'User 3' },
    ];

    res.status(200).json(users);
  } else if (req.method === 'POST') {
    const user = req.body;

    // Simulate database connection
    // await addUserToDatabase(user);

    res.status(201).json({ message: 'User added successfully.' });
  } else {
    res.setHeader('Allow', ['GET', 'POST']);
    res.status(405).end(`Method ${req.method} Not Allowed`);
  }
}

  1. The pages/api/users.js file defines an API route accessible at /api/users.
  2. The handler function accepts two parameters: req (a NextApiRequest object representing the HTTP request) and res (a NextApiResponse object for the HTTP response).
  3. For GET requests, it returns a user list (hardcoded here, but typically queried from a database in practice).
  4. For POST requests, it accepts user data from the request body, simulates adding it to a database, and returns a success message.
  5. For unsupported methods, it returns a 405 Method Not Allowed error with allowed methods specified.

Next.js API Routes handle JSON responses by default, but you can return other content types as needed. For example, use res.send to return HTML.


Tags

Next.js
React
JavaScript
TypeScript
CSS
Tailwind
Web Development
Frontend

Related Posts

අලුතෙන් Redesign උනු YouTube
Technology
Development
+1
අලුතෙන් Redesign උනු YouTube

Google විසින් YouTube video player එකට අලුත්ම redesign එකක් නිකුත් කරලා තියෙනවා. මේ update එකෙන් වීඩියෝ නැරඹීමේ අත්දැකීම දැන් smooth එකක් වෙලා තියෙනවා...

වසර 10කට අධික කාලයක් සේවය කර, සමුගන්නා Windows 10
News
Technology
වසර 10කට අධික කාලයක් සේවය කර, සමුගන්නා Windows 10

අවුරුදු 10ක් මුලුල්ලේ සේවය සැපයූ Windows 10 තමන්ගේ ගමන 2025 October 14 දිනෙන් අවසන් කරන බව Microsoft සමාගම නිළ වශයෙන් නිවේදනය කර ඇත. Windows 10 වලට අ...

අලුත්ම සෙවුම් ක්‍රමය Google AI Mode
News
Technology
අලුත්ම සෙවුම් ක්‍රමය Google AI Mode

ගූගල් තමන්ගේ අලුත් AI Mode එක දැන් ලොව පුරා launch කරලා තියෙනවා. මේ විශේෂාංගය කලින් US, India වගේ රටවල් වලට තමයි limited release එක දීලා තිබුණේ, හැබ...