简体   繁体   English

使 nextJS 忽略主机中的子路径

[英]make nextJS ignore a subPath in host

so the problem i'm having is, there is a directory in my public_html file named blog that is not related to my nextJs app, so basically after i deploy the app on host, everything works fine until i choose to check my blog part, for example the url below:所以我遇到的问题是,我的 public_html 文件中有一个名为 blog 的目录与我的 nextJs 应用程序无关,所以基本上在我在主机上部署应用程序之后,一切正常,直到我选择检查我的博客部分,例如下面的 url:

www.sth.com/blog

when i get there, i get the default next 404 page, the problem is, there is actual pages there, and i want my nodeJs server to ignore that exact route so when user goes to www.sth.com/blog , node app kind of ignore it and let it load the basic html pages.当我到达那里时,我得到了默认的下一个 404 页面,问题是,那里有实际页面,我希望我的 nodeJs 服务器忽略那个确切的路由,所以当用户转到www.sth.com/blog时,节点应用程序类型忽略它,让它加载基本的 html 页面。 i think it has something to do with my server.js file so here's the code in server.js also i hosted the app on cpanel if that's important.我认为这与我的server.js文件有关,所以这是 server.js 中的代码,如果这很重要,我也将应用程序托管在 cpanel 上。


const { createServer } = require('http')
const next = require('next')
 
const isDevMode = process.env.NODE_ENV !== 'production'
const port = process.env.PORT ? process.env.PORT : 3000
 
const nextjsApp = next({ dev: isDevMode })
const nextjsRequestHandler = nextjsApp.getRequestHandler()
 
nextjsApp
  .prepare()
  .then(() => {
    createServer((req, res) => {
      const url = new URL(req.url, "http://w.w")
      nextjsRequestHandler(req, res, url)
    }).listen(port, (err) => {
      if (err) throw err
    })
  })
  .catch((ex) => {
    console.error(ex.stack)
    process.exit(1)
  })

thank you in advance.先感谢您。

In your server configuration you have to check if request is meant for next or not.在您的服务器配置中,您必须检查请求是否用于下一个。 If not - you need to respond with your page, instead of passing request further to next:如果没有 - 您需要使用您的页面进行响应,而不是将请求进一步传递到下一个:

(req, res) => {
  const url = new URL(req.url, "http://w.w");
  if (/^\/blog\//.test(url.pathname)) {
    // send response with blog
  } else {
    // pass everything to Next
    nextjsRequestHandler(req, res, url);
  }
};

Another option would be to split next and not next in two different parts and route requests to them through reverse proxy.另一种选择是将下一个而不是下一个拆分为两个不同的部分,并通过反向代理将请求路由到它们。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM