Skip to content
Discussion options

You must be logged in to vote

@zoozalp I think you are talking about middlewares? If so here's an example of a simple one:

// hello.js

import { serialize } from "cookie";

const hello = (handler) => async (req, res) => {
      res.setHeader("Set-Cookie", serialize('hello', 'world'));
      return handler(req, res);
}

export { hello };

How you use it:

// pages/api/hello-cookie.js

import { hello } from './hello';

export default hello((req, res) => {
    return res.status(200).json({ message: "A cookie was set in this route by a middleware."})
});

If you are referring to set a middleware that affects all API routes I think that's not possible 🤔.

Replies: 7 comments 12 replies

Comment options

You must be logged in to vote
8 replies
@AxelReid
Comment options

@leerob
Comment options

@coltenkrauter
Comment options

@steven-tey
Comment options

@Esatollah
Comment options

Answer selected by steven-tey
Comment options

You must be logged in to vote
3 replies
@zoozalp
Comment options

@IRediTOTO
Comment options

@zoozalp
Comment options

Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
1 reply
@FPDK
Comment options

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Help
Labels
None yet