@remix-run/serve

Remix 应用服务器

¥Remix App Server

Remix 的设计旨在让你拥有自己的服务器,但如果你不想设置服务器,也可以使用 Remix 应用服务器。这是一个使用 Express 构建的、可用于生产环境的基本 Node.js 服务器。

¥Remix is designed for you to own your server, but if you don't want to set one up, you can use the Remix App Server instead. It's a production-ready but basic Node.js server built with Express.

根据设计,我们不提供自定义 Remix 应用服务器的选项,因为如果你需要自定义底层 express 服务器,我们宁愿你完全管理服务器,而不是创建一个抽象层来处理你可能需要的所有自定义操作。如果你发现需要自定义,则应使用 @remix-run/express 适配器。

¥By design, we do not provide options to customize the Remix App Server because if you need to customize the underlying express server, we'd rather you manage the server completely instead of creating an abstraction to handle all the possible customizations you may require. If you find you want to customize it, you should use the @remix-run/express adapter instead.

你可以在 packages/remix-serve/cli.ts 中看到底层 express 服务器配置。默认情况下,它使用以下 Express 中间件(有关默认行为,请参阅其文档):

¥You can see the underlying express server configuration in packages/remix-serve/cli.ts. By default, it uses the following Express middlewares (please refer to their documentation for default behaviors):

HOST 环境变量

¥HOST environment variable

你可以通过 process.env.HOST 为你的 Express 应用配置主机名,该值将在启动服务器时传递给内部 app.listen 方法。

¥You can configure the hostname for your Express app via process.env.HOST and that value will be passed to the internal app.listen method when starting the server.

HOST=127.0.0.1 npx remix-serve build/index.js
remix-serve <server-build-path>
# e.g.
remix-serve build/index.js

PORT 环境变量

¥PORT environment variable

你可以使用环境变量更改服务器的端口。

¥You can change the port of the server with an environment variable.

PORT=4000 npx remix-serve build/index.js

开发环境

¥Development Environment

根据 process.env.NODE_ENV,服务器将以开发模式或生产模式启动。

¥Depending on process.env.NODE_ENV, the server will boot in development or production mode.

server-build-path 需要指向 remix.config.js 中定义的 serverBuildPath

¥The server-build-path needs to point to the serverBuildPath defined in remix.config.js.

由于只需要将构建工件(build/public/build/)部署到生产环境,因此无法保证 remix.config.js 在生产环境中可用,因此你需要使用此选项告知 Remix 你的服务器构建位置。

¥Because only the build artifacts (build/, public/build/) need to be deployed to production, the remix.config.js is not guaranteed to be available in production, so you need to tell Remix where your server build is with this option.

在开发过程中,remix-serve 将通过清除每个请求的 require 缓存来确保运行最新代码。这对你的代码有一些影响,你可能需要注意:

¥In development, remix-serve will ensure the latest code is run by purging the require cache for every request. This has some effects on your code you might need to be aware of:

  • 模块范围内的任何值都将为 "reset"。

    ¥Any values in the module scope will be "reset"

    // this will be reset for every request because the module cache was
    // cleared and this will be required brand new
    const cache = new Map();
    
    export async function loader({
      params,
    }: LoaderFunctionArgs) {
      if (cache.has(params.foo)) {
        return json(cache.get(params.foo));
      }
    
      const record = await fakeDb.stuff.find(params.foo);
      cache.set(params.foo, record);
      return json(record);
    }
    

    如果你需要在开发过程中保留缓存的解决方法,你可以在服务器中设置 singleton

    ¥If you need a workaround for preserving cache in development, you can set up a singleton in your server.

  • 任何模块副作用都将保留!这可能会导致问题,但无论如何都应该避免。

    ¥Any module side effects will remain in place! This may cause problems but should probably be avoided anyway.

    import { json } from "@remix-run/node"; // or cloudflare/deno
    
    // this starts running the moment the module is imported
    setInterval(() => {
      console.log(Date.now());
    }, 1000);
    
    export async function loader() {
      // ...
    }
    

    如果你需要以具有此类模块副作用的方式编写代码,则应该设置自己的 @remix-run/express 服务器和一个正在开发的工具(例如 pm2-dev 或 nodemon),以便在文件更改时重新启动服务器。

    ¥If you need to write your code in a way that has these types of module side effects, you should set up your own @remix-run/express server and a tool in development like pm2-dev or nodemon to restart the server on file changes instead.

在生产环境中,这种情况不会发生。服务器启动,一切就此结束。

¥In production this doesn't happen. The server boots up, and that's the end of it.

Remix v2.17 中文网 - 粤ICP备13048890号