npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

nextjs-middleware-api

v2.0.6

Published

simple nextjs middleware for api routes

Downloads

10

Readme

nextjs-middleware-api

simple nextjs middleware for api routes

Description

nextjs-middleware-api gives you a simple way to add custom middleware functions or using npm middleware

like cors and express-rate-limit and cookie-parser and ...more

Install

$ npm install nextjs-middleware-api

Usage with custom middleware

Simple usage with nextjs api route.

/pages/api/account route example

// import package
import NextMiddleware from 'nextjs-middleware-api'

// import your cutom middleware
import Auth from './customMiddleware/auth'

// add your custom middleware and callback function
export default NextMiddleware(Auth, (req, res) => {
    res.send("Hello mr " + req.username)
})

/middleware/auth.js custom middleware file

// you can catch [req, res, next] to handle your custom middleware function
function Auth(req, res, next) {
    // 1- get token from [req]
    // 2- check token validation
    // 3- return response

    // dummy example
    // if token is valid return next() function
    if (1 == 1) 
    {
        // do your work
        req.username = "John Doe";
        
        // after you finish just return next() to continue to the next middleware if exist or your final callback function
        next();
    }
    else
    {
        // if token not valid for some reason return your custom error message with custom status code
        res.status(404).send({ success: false, message: 'username or password is wrong' });
    }
}

export default Auth;

Usage with npm middleware packages like cors

you can add cors to middleware by just pass cors like this

// import package
import NextMiddleware from 'nextjs-middleware-api'

// import your cutom middleware
import Auth from './customMiddleware/auth'
import Validation from './customMiddleware/validation'

// import third party packages like cors
import cors from 'cors'

// using the middleware like
const usingCors = cors({
    origin: 'http://example.com', // add your domain or use "*" to accept all origins
    methods: "GET,HEAD,PUT,PATCH,POST,DELETE",
    optionsSuccessStatus: 200 // some legacy browsers (IE11, various SmartTVs) choke on 204
    // ... more cors options
})

export default NextMiddleware(usingCors, Auth, Validation, (req, res) => {
    res.send("Hello mr " + req.username)
})

you can add more packages with the same way

How to allow only selected methods

use can allow methods like POST or GET or Delete or any other type of method by just pass allowedMethods inside object like this

// import package
import NextMiddleware from 'nextjs-middleware-api'

// import your cutom middleware
import Auth from './customMiddleware/auth'
import Validation from './customMiddleware/validation'
import IsAdmin from './customMiddleware/isAdmin'

var customOptions = {
    allowedMethods: ["POST", "get", "DelEtE"], // not case sensitive
}

export default NextMiddleware(customOptions, Auth, Validation, IsAdmin,  (req, res) => {
    if (req.method == "POST") 
    {
        res.send("Request POST method")
    }
    else if (req.method == "GET") 
    {
        res.send("Request GET method")
    }
    // final else will be [DELETE] method because there is no fourth option in [allowedMethods] array
    else 
    {
        res.send("Request DELETE method")
    }
})

allowedMethods must be an array[] of sting

allowedMethods not case sensitive you can type ["POST"] or ["post"] or ["PoSt"]

allowedMethods will return 404 page not found for methods not included in array