Next.js的轻巧中间件和路线处理解决方案由Dirext提供动力?
$ npm install connext-js
初始化连接的新实例。
const Connext = require ( 'connext-js' ) ;
const app = Connext ( ) ;CONNEXT是Next.js的中间件解决方案,并带有Express-Style语法,该语法支持全局和灵活的路线特定中间件。对于全局中间件,您必须创建一个必须包含global.js控制器文件的controllers文件夹。我们建议还为您的其他中间件创建控制器文件,以使您的API逻辑模块化。
使用connext的设置路由非常类似于Express中的设置路由。
所有有效的HTTP请求方法都在连接对象上都有关联的方法。
app . get ( ) ;
app . post ( ) ;
app . put ( ) ;
app . delete ( ) ;
app . head ( ) ;
app . trace ( ) ;
app . patch ( ) ;
app . options ( ) ;
app . connect ( ) ;连接支持任何静态或查询的API端点。对动态路由的支持即将到来。 ⚡️
在connext中,您可以访问Next.js中可用的请求对象,并且该访问通过中间件链持续存在 - 就像Express一样!可用的属性是req.url , req.method和req.body 。
与Express不同,如果您需要存储数据,则可以使用想要存储的任何数据将任何键添加到响应对象中。这将简单地增加您的响应对象,并继续在请求的整个生命周期中持续存在此对象。前任:
response . data = JSON . stringify ( data ) ;
response . match = true ;
response . array = [ 'I' m the data you need '];示例文件结构:
├── ...
├── controllers
│ ├── global.js # required for global middleware
│ └── middleware.js # suggested for modularization of middleware functions
├── pages # required folder for routes in Next.js
│ └── api # required folder for API routes in Next.js
└── ...
要利用Connext的全局中间件功能,您必须在称为controllers文件夹中创建一个global.js文件。 controllers文件夹必须与您的pages文件夹处于同一级别,并且您的global.js文件必须导出数组。
connext具有一个简单的全局内置错误处理程序,每当某物传递到next()的调用中时,它将运行。如果您想使用自己的错误处理程序,请在global.js中定义它为导出数组的最后一个元素。
global.js示例
// a global middleware function
const globalOne = ( req , res , next ) => {
console . log ( 'this the first function that runs!' ) ;
return next ( ) ;
} ;
// another global middleware function
const globalTwo = ( req , res , next ) => {
console . log ( 'another one!' ) ;
return next ( ) ;
} ;
// global error handler
const errorHandler = ( err , req , res , next ) => {
return res . status ( 500 ) . send ( 'an error occurred' ) ;
} ;
// export your array of global middleware
module . exports = [ globalOne , globalTwo , errorHandler ] ;我们建议您在controllers文件中的一个或多个文件中对其他中间件进行模块化,以使您的代码可读且易于调试?
Middleware.js示例
const middlewareController = { } ;
middlewareController . functionOne = ( req , res , next ) => {
// middleware functionality here
return next ( ) ;
}
middlewareController . functionTwo = ( req , res , next ) => {
// middleware functionality here
return next ( ) ;
}
module . exports = middlewareController ;像Express一样,Connext中的每个方法都具有与有效的HTTP方法相关的字符串。
例如: GET, DELETE, POST ,等等。
要使用Connext定义路由,请在Next.js所需的api文件夹中添加一个JavaScript文件。
├── pages # required folder for routes in Next.js
│ └── api # required folder for API routes in Next.js
│ └── exampleRoute.js # created route file inside of API folder
路由文件的内部
const Connext = require ( 'Connext-js' ) ;
const middleware = require ( '../../controllers/middleware' ) ;
const app = Connext ( ) ;
app . get ( '/api/exampleRoute' , middleware . one , middleware . two , ( req , res ) => {
res . status ( 200 ) . json ( res . example ) ;
} ) ;
app . post ( '/api/exampleRoute' , middleware . three , ( req , res ) => {
res . status ( 200 ) . json ( res . example ) ;
} ) ;
app . delete ( '/api/exampleRoute' , middleware . four , ( req , res ) => {
res . status ( 200 ) . json ( res . example ) ;
} ) ;
export default app ;萨拉·鲍尔斯(Sara Powers)
Eli Gallipoli
Izumi Sato
亚历克斯·康