[英]Prevent the creation of routes from components and public folders in the pages folder. And deny access to see files in public folders. NextJs
I have NextJs application with a custom server where I have customized a route.我有一个带有自定义服务器的 NextJs 应用程序,我在其中自定义了一条路线。 Where I show the template depending on the hostname and pathname.
我根据主机名和路径名显示模板的位置。
My server.js我的 server.js
const next = require('next');
const { createServer } = require('http');
const { parse } = require('url');
const dev = process.env.ENV === 'local';
const app = next({ dev });
const handle = app.getRequestHandler();
const port = 3000;
app.prepare().then(() => {
createServer(async (req, res) => {
const parsedUrl = parse(req.url, true);
const { pathname, query } = parsedUrl;
const pathnameFromServer = 'test_path_name';
const templateFromServer = 'template1';
if (pathname === `/${pathnameFromServer}`) {
await app.render(req, res, `/${templateFromServer}`, query);
} else {
await handle(req, res, parsedUrl);
}
}).listen(port, (err) => {
if (err) throw err;
console.log('> Ready on http://localhost:3000');
});
});
I am creating a template in the pages folder.我正在页面文件夹中创建一个模板。 And I want to create the components, public folder in it.
我想在其中创建组件,公用文件夹。 Like this.
像这样。
But NextJs build everything in the pages folder as routes.但是 NextJs 将 pages 文件夹中的所有内容构建为路由。
My next.config.js我的 next.config.js
const withTM = require('next-transpile-modules')(['@nfs-react/components']);
module.exports = withTM({
useFileSystemPublicRoutes: false,
});
I need to prevent the creation of routes from components and public folders in the pages folder.我需要防止从页面文件夹中的组件和公用文件夹创建路由。 I also need to deny access to see files in public folders.
我还需要拒绝访问以查看公用文件夹中的文件。
You can name the /pages
folder something else, so Next.js will ignore it.您可以将
/pages
文件夹命名为其他名称,因此 Next.js 将忽略它。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.