简体   繁体   English

NextJS 路由:为什么需要不同的客户端和服务器路由?

[英]NextJS routing: why do there need to be different client and server routes?

I'm somewhat new to React, but wholly new to NextJS, which I'm trying to teach myself.我对 React 有点陌生,但对 NextJS 完全陌生,我正在努力自学。 I've been going through the NextJS 'getting started' tutorial, as well as looking at some other tutorials.我一直在浏览 NextJS“入门”教程,以及查看其他一些教程。 I don't understand why there is a need to distinguish between client routes and routes on the server, that is, why the client route given as an example uses a query, whereas the server route does not.我不明白为什么需要区分客户端路由和服务器上的路由,即为什么作为示例给出的客户端路由使用查询,而服务器路由则没有。 I know that I am not seeing the forest for the trees, so if anyone can point me in the right direction of 'grokking' NextJS routes, I'd appreciate it.我知道我只见树木不见森林,所以如果有人能指出我“grokking”NextJS 路线的正确方向,我将不胜感激。

From this tutorial, on the client side we might have教程中,在客户端我们可能有

<Link href={`/blog?slug=${slug}`} as={`/blog/${slug}`} prefetch>
  ...
</Link>

which requires us (it would seem) to set up an Express server and handle the route这需要我们(看起来)设置一个 Express 服务器并处理路由

/blog/:slug

OK.好的。 But why?但为什么? Why isn't the local link simply为什么本地链接不简单

<Link href={`/blog/${slug}`}  prefetch>
      ...
    </Link>

? Or, alternatively, why doesn't NextJS handle server-side the route /blog?slug=${slug} ?或者,为什么 NextJS 不在服务器端处理路由/blog?slug=${slug}

I can follow what the NextJS site 'getting started' tutorial (I input the code myself and test it) is doing, but as far as routing I am a bit lost as to what I'm doing and why .我可以按照 NextJS 网站的“入门”教程(我自己输入代码并对其进行测试)进行操作,但就路由而言,我有点迷失了我在做什么以及为什么 Clearly I am missing a crucial (and elementary) element here, and would appreciate clues as to the error of my ways.显然,我在这里遗漏了一个关键(和基本)元素,并且希望能提供有关我的方法错误的线索。

If you look at the route如果你看路线

/blog/${slug}

Here slug can take different values as it a parameter.这里 slug 可以采用不同的值作为参数。 If you want NextJs to handle such routes you need to implement a route for each value that slug can take.如果您希望 NextJs 处理此类路由,您需要为 slug 可以采用的每个值实现一个路由。 For example.例如。

/blog/slug1
/blog/slug2
/blog/slug3

And this number can grow very quickly.而且这个数字可以增长得非常快。 Hence we use an Express server so that we can intercept request to route /blog and pass slug as parameter to the blog page.因此,我们使用 Express 服务器,这样我们就可以拦截对路由/blog的请求,并将slug作为参数传递给blog页面。

You do not need different client and server routes.您不需要不同的客户端和服务器路由。 That is not compulsory unless you are using client-side routing explained here , with dynamic routes.这不是强制性的,除非您使用此处解释的客户端路由和动态路由。 If you do not want to use that you can switch for server-side routing with our old friend <a> which does not require separate links.如果你不想使用它,你可以使用我们的老朋友<a>切换到服务器端路由,它不需要单独的链接。

NextJS handles server-side route queries. NextJS 处理服务器端路由查询。 Below is a simple example for that -下面是一个简单的例子 -

server.get("/dashboard", (req, res) => {
    const actualPage = '/dashboard';
    const queryParams = {
        username: req.query.username
    };
    app.render(req, res, actualPage, queryParams);
  });

In above example, when you open page - /dashboard?username=amit , you can get the value passed in URL query in your page which you can retrieve using - getInitialProps function.在上面的示例中,当您打开页面 - /dashboard?username=amit时,您可以在页面中获取通过 URL 查询传递的值,您可以使用 - getInitialProps函数检索该值。 To get data from URL query with server-side routing, you must create your page with getInitialProps function.要使用服务器端路由从 URL 查询中获取数据,您必须使用getInitialProps函数创建页面。 Example is as below --例子如下——

Dashboard.getInitialProps = async(ctx)  => {

    const query = ctx.query;

    const username = query.username;

    return {"username": username};

}

Above code sends data from url query as page props.上面的代码将来自 url 查询的数据作为页面道具发送。 Server returns that data to the page in req .服务器将该数据返回到req中的页面。

Now you must create _app page with getInitialProps function to make this work.现在您必须使用getInitialProps函数创建_app 页面才能完成这项工作。

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

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