Rsbuild includes a built-in dev server that enhances the development experience. When you run rsbuild dev or rsbuild preview, the server starts and provides features like page preview, routing, and hot module replacement.
By default, the Rsbuild server's base path is /. You can access output files like index.html and public folder assets at http://localhost:3000/.
You can modify the server's base path using server.base. For example, to access files at http://localhost:3000/foo/, configure it as follows:
After starting the dev server, you can access /rsbuild-dev-server to view all static assets generated during the current build.
For example, open http://localhost:3000/rsbuild-dev-server in your browser:
The Rsbuild server provides default routing conventions and allows customization through configuration.
The Rsbuild server generates page routes based on the server.base and source.entry configurations.
When the entry is index, you can access the page at /. When the entry is foo, you can access the page at /foo.
When server.base is /base, you can access the index page at /base, and the foo page at /base/foo.
By default, requests that meet the following conditions fall back to index.html when the corresponding resource is not found:
GET or HEAD requesttext/html (the request header accept type is text/html or */*)If Rsbuild's default server.htmlFallback configuration doesn't meet your needs (for example, if you want to access main.html when accessing /), you can configure it using server.historyApiFallback.
Normally, / points to the dist root directory, and HTML files are output to the dist root directory. In this case, you can access HTML pages at /some-path.
If you output HTML files to other subdirectories by modifying output.distPath.html, you need to access HTML pages at /[htmlPath]/some-path.
For example, if you set HTML files to output to the HTML directory, you can access index.html at /html/, and foo.html at /html/foo.
Rsbuild has a built-in lightweight dev server that differs from the dev servers provided by Rspack CLI or webpack CLI, including different configuration options.
Compared to the dev server in Rspack CLI, Rsbuild's dev server has the following differences:
connect, which has fewer dependencies and faster startup than express used by @rspack/dev-server.Rsbuild does not support using Rspack's devServer config. Instead, use Rsbuild's dev and server configs.
In Rsbuild, the dev config contains settings that only apply in development mode, while the server config applies to both dev and preview servers.
Below are the Rsbuild configuration options that correspond to Rspack CLI's devServer config:
For more configurations, refer to Config Overview.
Rsbuild's middleware implementation is built on connect, a lightweight HTTP server framework, and uses the standard Node.js request and response objects for handling HTTP interactions.
Rsbuild provides three ways to register middleware:
use method to register middleware.When migrating from other server frameworks (such as Express), the original middleware may not work directly in Rsbuild. For example, Express-specific properties like req.params, req.path, req.search, and req.query are not available in Rsbuild middleware.
If you need to reuse existing middleware in Rsbuild, you can integrate the entire server application as middleware using the following approach:
If you want to integrate Rsbuild's dev server into a custom server, you can use the createDevServer method to get the Rsbuild dev server instance and call its methods as needed.
For details, refer to Rsbuild - createDevServer.