
Product
Announcing Socket Fix 2.0
Socket Fix 2.0 brings targeted CVE remediation, smarter upgrade planning, and broader ecosystem support to help developers get to zero alerts.
nextjs-api-router
Advanced tools
Lightweight tool to build clean restful API routes for your Next.js application.
Lightweight router class to build spotless (restful) API route handlers for your Next.js server application.
# npm
$ npm install next-js-api-router
# yarn
$ yarn add nextjs-api-router
First create a controller object that contains all handlers for the HTTP methods you want to handle on your specific route.
// API controller: controllers/example/index.ts
import type { Controller } from "nextjs-api-router";
const controller: Controller = {
GET: (req, res) => {
res.send("Hello world from nextjs-api-router!");
},
POST: (req, res) => {
res.send(req.body);
},
};
export default controller;
Import the controller in a new router class in the API handler file in /pages/api/%your-route%
. Export the router.handle
function by default to make your API work.
// API handler: pages/api/example.ts
import { Router } from "nextjs-api-router";
import controller from "../controllers/example";
const router = new Router(controller);
export default router.handle();
The Router
class manages the controller object inside the class itself. By instantiating it can optionally be given a controller
object in the constructor.
The controller can be modifiied all time with the router.controller
property of the class. For example:
router.controller.GET = (req, res) => {
res.send("");
};
The router.handle
function returns a function that handles the configured HTTP method handlers when sending a request to the server.
By passing handlers as arguments to the router.use
method It's also possible to create middleware functions on the Router
class. This middleware handler will be executed on every request to the route.
const router = new Router(controller);
const logger = (req, res, next) => {
console.log(req.method, Date.now()
next();
};
router.use(logger);
A controller object can be passed to the Router
class constructor. It contains all the handlers for the HTTP methods used in the router. These handlers can be assigned singular or as an array (like middleware).
import type { Controller, Handler } from "nextjs-api-router";
const verify: Handler = (req, res, next) => {
// do some verification here
next();
}
const controller: Controller {
// Just single handler functions
GET: (req, res) => {res.send("Hello world!")},
DELETE: (req, res) => {res.send("Goodbye world...")},
// Multiple handlers
POST: [verify, (req, res) => {
res.send("You're verified now!")
}],
}
The handler function is a function called inside the router. It has access to the request and the response of the server, and can be used as middleware, using the next
parameter, to execute the next handler in series.
import { Router } from 'nextjs-api-router';
import type { Controller, Handler } from "nextjs-api-router";
const logger: Handler = (req, res, next) => {
console.log(req.method, Date.now()
next();
};
const reply: Handler = (req, res, next) => {
res.send("You got this!")
};
const controller: Controller {
GET: [logger, reply]
}
const router = new Router();
// Controller can also be passed in as parameter of the handle function
export default router.handle(controller);
FAQs
Lightweight tool to build clean restful API routes for your Next.js application.
We found that nextjs-api-router demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Product
Socket Fix 2.0 brings targeted CVE remediation, smarter upgrade planning, and broader ecosystem support to help developers get to zero alerts.
Security News
Socket CEO Feross Aboukhadijeh joins Risky Business Weekly to unpack recent npm phishing attacks, their limited impact, and the risks if attackers get smarter.
Product
Socket’s new Tier 1 Reachability filters out up to 80% of irrelevant CVEs, so security teams can focus on the vulnerabilities that matter.