next-api-backend
v0.0.2
Published
This project is a backend server built for Next.js.
Downloads
5
Readme
next-api-backend
This project is a backend server built for Next.js.
Features
The server has the following features:
Middleware: The server supports the use of middleware, which are functions that are executed before the main route is handled. Middlewares can be defined with the
defineMiddleware
function, which takes a middleware or a list of middlewares, along with optional parameters and options.Routes: Routes are defined with the
defineRoute
function, which takes a route function and an optional list of middlewares. This function returns a new function that handles the route and executes the middlewares.Context: The server maintains a global context object, which can be accessed and modified with the
getContext
andsetContext
functions.
Use
To define a middleware, you can use the defineMiddleware
function as follows:
const myMiddleware = defineMiddleware(
(context) => {
// Your code here
},
[param1, param2],
{ includes: ['route1', 'route2'], excludes: ['route3'], verbs: ['GET'] }
)
To define a route, you can use the defineRoute
function as follows:
export const GET = defineRoute(
(request, params) => {
// Your code here
},
[myMiddleware]
)
To access the context object, you can use the getContext
function as follows:
const value = getContext('key')
To modify the context object, you can use the setContext
function as follows:
setContext('key', value)
Contribute
If you would like to contribute to this project, please fork the repository and submit a pull request with your changes.
License
This project is licensed under the terms of the MIT License.