DEV Community

Cover image for how you can create custom API routes using Next.js
Shohrab Hossain
Shohrab Hossain

Posted on

how you can create custom API routes using Next.js

As of my knowledge cutoff in September 2021, Next.js version 12 was the latest release, and it did not support decorators for API routes. However, if Next.js 13 introduces decorator support for API routes, I may not be aware of it.

The file-based naming convention is an integral part of Next.js API routes, where each file in the pages/api directory represents an API endpoint. For example, pages/api/users.ts would correspond to the /api/users endpoint.

If you want to have more control over the routing logic, you could consider using a framework like Express.js alongside Next.js. With Express.js, you can use decorators or middleware to define custom routes and handle API requests. However, keep in mind that combining Next.js with an external server framework like Express.js may require additional configuration and might not be the recommended approach.

I recommend referring to the official Next.js documentation, release notes, and community discussions to stay up to date with the latest features and changes in Next.js, including any updates regarding decorator support for API routes in Next.js 13.

Certainly! Here's an example of how you can create custom API routes using Next.js and Express.js:

First, you'll need to install the required dependencies:

npm install next express
Enter fullscreen mode Exit fullscreen mode

Next, create a custom server file called server.js in the root of your Next.js project:

// server.js
const express = require('express');
const next = require('next');

const dev = process.env.NODE_ENV !== 'production';
const app = next({ dev });
const handle = app.getRequestHandler();

app.prepare().then(() => {
  const server = express();

  // Custom API route
  server.get('/api/users', (req, res) => {
    // Handle the API logic here
    const users = [{ name: 'John' }, { name: 'Jane' }];
    res.json(users);
  });

  // Default Next.js request handler
  server.all('*', (req, res) => {
    return handle(req, res);
  });

  server.listen(3000, (err) => {
    if (err) throw err;
    console.log('> Ready on http://localhost:3000');
  });
});
Enter fullscreen mode Exit fullscreen mode

In this example, we're creating a custom API route for /api/users using Express.js. When a GET request is made to this route, it responds with a JSON array of users.

To start the server, you can update your package.json file with the following scripts:

{
  "scripts": {
    "dev": "node server.js"
  }
}
Enter fullscreen mode Exit fullscreen mode

Now, when you run npm run dev, the custom server will be started, and you can access the API route at http://localhost:3000/api/users.

Remember that this approach combines Next.js with an external server framework (Express.js). It gives you more control over routing and allows you to use decorators and other features provided by Express.js. However, it's important to consider the implications and trade-offs of introducing an external server framework to your Next.js project.

Top comments (0)