简体   繁体   English

SvelteKit、Next.js 和 Nuxt.js 代码在哪里运行? 你能在这些框架中编写 http 请求处理程序吗?

[英]Where does SvelteKit, Next.js and Nuxt.js code run? And can you write http request handlers in these frameworks?

I have been using SvelteKit casually for a little bit now in my own personal projects.在我自己的个人项目中,我一直在随便使用 SvelteKit。 I have used other frameworks like Next.js and Nuxt.js in the past too.我过去也使用过其他框架,例如 Next.js 和 Nuxt.js。 It has always confused me when working in these frameworks where the code I am writing is actually run.在我正在编写的代码实际运行的这些框架中工作时,它总是让我感到困惑。

For some context in my day job I use the ASP.NET Core platform to create more traditional web applications.在我的日常工作中,我使用 ASP.NET 核心平台来创建更传统的 web 应用程序。

In ASP.NET it is clear to me where the code I write is run.在 ASP.NET 中,我很清楚我编写的代码在哪里运行。 I write frontend markup in Razor pages (just a way to write html but inject C# expressions inside).我在 Razor 页面中编写前端标记(只是编写 html 但在内部注入 C# 表达式的一种方法)。 When the browser requests a page to view from the server, the server grabs the page and any data it needs, generates html from the Razor page markup and sends it back to the browser to render it into a beautiful web page Also within the same application I can write http handlers that can listen out for form submissions and interact with a database via an ORM.当浏览器向服务器请求要查看的页面时,服务器获取该页面和它需要的任何数据,从 Razor 页面标记生成 html 并将其发送回浏览器以将其呈现为漂亮的 Z2567A5EC9705EB7AC2C984033 中的页面我可以编写 http 处理程序,它可以监听表单提交并通过 ORM 与数据库交互。

In JavaScript frameworks such as SvelteKit, Next.js or Nuxt.js however this process has always confused me.在 SvelteKit、Next.js 或 Nuxt.js 等 JavaScript 框架中,但这个过程总是让我感到困惑。 I know that each of the frameworks use their respective component frameworks (Svelte, React and Vue) under the hood in some way.我知道每个框架都以某种方式在后台使用它们各自的组件框架(Svelte、React 和 Vue)。 I also understand that each framework adds the ability to have file based routing so that files in a specific place in the src code map to a specific route in the browser.我还了解每个框架都添加了基于文件的路由的功能,以便 src 代码 map 中特定位置的文件到浏览器中的特定路由。 eg in SvelteKit you can have a svelte file at src/routes/myAmazingPage.svelte that gets rendered in when the browser browses to https://wherevertheappishosted.com/myAmazingPage .例如,在 SvelteKit 中,您可以在src/routes/myAmazingPage.svelte拥有一个 svelte 文件,当浏览器浏览到https://wherevertheappishosted.com/myAmazingPage时,该文件会被渲染。

However this is where my understanding becomes a bit clouded.然而,这是我的理解变得有点模糊的地方。 When you browse between pages in SvelteKit it does not cause page reloads in the browser.当您在 SvelteKit 中的页面之间浏览时,它不会导致浏览器中的页面重新加载。 This tells me that the framework is using some kind of client side routing to handle this behavior as the url at the top of the page and the page contents change without causing a page reload.这告诉我,该框架正在使用某种客户端路由来处理此行为,因为页面顶部的 url 并且页面内容发生更改而不会导致页面重新加载。 There are also no network requests being sent to the server to fetch pages at a certain url which tells me that all the data fetching must be being done in JavaScript on component mount or within some other lifecycle hook.也没有网络请求被发送到服务器以获取某个 url 的页面,这告诉我所有数据获取必须在 JavaScript 组件安装或其他一些生命周期挂钩中完成。

So my question is in that case if there are no http requests being sent out per page request (ie when you navigate to a different page) to a server, then where is the code I write in SvelteKit actually run?所以我的问题是在这种情况下,如果没有 http 请求被发送到每个页面请求(即当您导航到不同的页面时)到服务器,那么我在 SvelteKit 中编写的代码实际运行在哪里? Is it the case of all the code I write in SvelteKit is compiled and sent over to the browser on initial page load and then all run in the browser?是不是我在 SvelteKit 中编写的所有代码都在初始页面加载时编译并发送到浏览器,然后全部在浏览器中运行? Including code I write to fetch data within lifecycle methods like onMount() .包括我编写的用于在onMount()等生命周期方法中获取数据的代码。

Also if that is true then do these frameworks have any kind of http server running in the background to handle any other web requests?此外,如果这是真的,那么这些框架是否有任何类型的 http 服务器在后台运行以处理任何其他 web 请求? For example can I write a http handler in svelte kit to handle a post request from a form submission to save data to a database?例如,我可以在 svelte 工具包中编写一个 http 处理程序来处理来自表单提交的发布请求以将数据保存到数据库吗? That is the way I would go about handling form submissions in ASP.NET and I have always wondered if it is possible to do in SvelteKit, Next.js or Nuxt.js. That is the way I would go about handling form submissions in ASP.NET and I have always wondered if it is possible to do in SvelteKit, Next.js or Nuxt.js. The only alternative to this that I have used in the past is writing a separate http server written in node.js and express to handle communicating with a database and other operations, that I send all my requests from my SvelteKit apps towards.我过去使用的唯一替代方法是编写一个单独的 http 服务器,用 node.js 编写并表示处理与数据库和其他操作的通信,我将所有请求从我的 SvelteKit 应用程序发送到。

May be a bit of an obvious question to more experienced JavaScript framework developers out there, but since working in a more traditional MVC framework like ASP.NET on a daily basis this subject has always confused me.对于更有经验的 JavaScript 框架开发人员来说,这可能是一个明显的问题,但是由于每天都在像 ASP.NET 这样更传统的 MVC 框架中工作,所以这个主题总是让我感到困惑。

Thanks for reading my first Stack Overflow Question,it was a long one so appreciate you making it to the end.感谢您阅读我的第一个 Stack Overflow 问题,这是一个很长的问题,非常感谢您完成它。 Have a good day !祝你有美好的一天 !

SvelteKit applications can be deployed few different ways to production. SvelteKit 应用程序可以以几种不同的方式部署到生产环境。

  • In development mode, the request handler is run by Vite development server在开发模式下,请求处理程序由Vite 开发服务器运行
  • In static production deployment mode, there is no request handler and nothing is run on the backend - the visitor just downloads HTML, CSS and JS files from the server在 static 生产部署模式下,没有请求处理程序,后端也不运行任何内容 - 访问者只需从服务器下载 HTML、CSS 和 JS 文件
  • In server-side rendering (SSR) production mode, the requests are run by SvelteKit node adapter (Node.js process) .在服务器端渲染 (SSR) 生产模式下,请求由SvelteKit 节点适配器(Node.js 进程)运行。 The adapter allows also to write server-side request handlers that are not available in the static deployment mode.该适配器还允许编写在 static 部署模式中不可用的服务器端请求处理程序。

Next.js and Nuxt.js have their respective own web servers written in JavaScript on the top of Node.js V8 virtual machine. Next.js and Nuxt.js have their respective own web servers written in JavaScript on the top of Node.js V8 virtual machine.

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

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