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
亞歷克斯·康