[Next.js] Add Middleware to an API Route Created with next-connect
2023-09-14 08:59:12 时间
We'll learn how to create a middleware function for next-connect. This middleware will work at the route level, for example, for every request that hits the /api/next-connect-middleware endpoint, or at the HTTP verb level, GET /api/next-connect-middleware
import nextConnect from 'next-connect' const withAuthentication = (req, res, next) => { if (!req.headers.authentication) { return res.status(401).json({message: 'error'}) } return next() } const handler = nextConnect() .use(withAuthentication) .get(withAuthentication , (req, res) => res.json({message: 'get'})) export default handler
相关文章
- JS原生复制
- js刷新当前页面的5种方式
- 性能优化之YUICompressor压缩JS、CSS
- vue.js客服系统实时聊天项目开发(十三)日期缩短展示,同一天只展示时秒,同一年展示月日小时秒
- 【Android 应用开发】Android 网络编程 API笔记 - java.net 包相关 接口 api
- 判断滚动条到底部的JS代码详解编程语言
- Linux加密API:安全加固保护您的数据(linux加密api)
- Oracle:是否具备API?(oracle有api吗)
- Linux文件系统API的运用(linux文件操作api)
- 快速掌握JS操作MySQL数据库技巧(js操作mysql数据库)
- Node.js使用初印象
- 使用JS实现Redis数据读取(js读取redis)
- 使用C语言搭建MySQL数据库API(c语言mysql api)
- 使用MSSQL的API实现更佳的数据库管理(mssql的api)
- 数据一步一步学习如何用C API导出MySQL数据(c api导出mysql)
- JS实时链接Oracle让数据库访问更便捷(js实时连接oracle)
- Redis实现API调用计数限制(redis限制api次数)
- 用js解决数字不能换行问题
- js判断是否为数组的函数:isArray()
- 远离JS灾难css灾难之js私有函数和css选择器作为容器
- 百度地图api应用标注地理位置信息(js版)
- js控制web打印(局部打印)方法整理
- 用js来获取上传的文件名纯粹是为了美化而用
- JS关键字球状旋转效果的实例代码
- JS嵌套函数调用上下文的问题解决