【发布时间】:2021-08-31 13:01:29
【问题描述】:
有没有办法拒绝访问 NextJS 中的目录?例如,如果我想禁止访问目录 /public/images ?
我会通过 api 路由来服务图像,所以图像在组件中看起来像这样:
<Image src="/api/image1" alt="Logo" width={720} height={160} />
和image1 api路由:
res.setHeader('Content-Type', 'image/jpg');
const imageBuffer2 = await (await fetch('http://example.com/images/logo.jpg')).body;
res.send(imageBuffer2)
但用户将无法通过在浏览器中输入"example.com/images/logo.jpg" 来访问该图像。
我的目标是只为登录的用户提供图像。在 PHP 中类似的任务很容易,但是如何在 NextJS 中实现呢?
谢谢!
【问题讨论】: