简体   繁体   English

防止从 pages 文件夹中的组件和公用文件夹创建路由。 并拒绝访问查看公用文件夹中的文件。 NextJs

[英]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.

相关问题 将文件从differend文件夹移动到ONE文件夹。 没有子文件夹。 吞 - Move files from differend folders to ONE folder. Without subfolders. Gulp express.static() 不从不是“/”的路由器路径中的公共文件夹中提供文件 - express.static() not serving files from public folders in router paths that are not "/" Laravel 文件/文件夹未显示在公共目录中 - Laravel files/folders not showing up in Public directory 从 express 中的路由文件夹访问公共静态文件 - Acessing public static files from routes folder in express 如何从NextJS的公共文件夹中获取()? - How to fetch() from public folder in NextJS? 如何保护public_html中的文件夹免受访问者的攻击 - How to protect folders in public_html from visitors 有没有办法动态访问我的Github Pages服务器上托管的文件和文件夹? - Is there a way to dynamically access files and folders hosted on my Github Pages server? React JS 样式组件从公共文件夹导入图像 - React JS styled-components importing images from public folder node.js express 如何访问我的公共文件夹中的文件 - How to access files in my public folder in node.js express Google Drive API中父文件夹下的文件和文件夹列表 - List of files and folders under a parent folder from Google Drive API
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM