@dsinni/connect-history-api-fallback

中文文档
1.6.0发布于 2021/09/28MITCommonJS零依赖近 30 天下载 3422

Provides a fallback for non-existing directories so that the HTML 5 history API can be used.

原始使用文档
文档目录9 节
  1. Table of Contents
  2. Introduction
  3. Usage
  4. Options
  5. index
  6. rewrites
  7. verbose
  8. htmlAcceptHeaders
  9. disableDotRule

connect-history-api-fallback

Middleware to proxy requests through a specified index page, useful for Single Page Applications that utilise the HTML5 History API.

Build Status Dependency Status

NPM

Table of Contents

Introduction

Single Page Applications (SPA) typically only utilise one index file that is accessible by web browsers: usually index.html. Navigation in the application is then commonly handled using JavaScript with the help of the HTML5 History API. This results in issues when the user hits the refresh button or is directly accessing a page other than the landing page, e.g. /help or /help/online as the web server bypasses the index file to locate the file at this location. As your application is a SPA, the web server will fail trying to retrieve the file and return a 404 - Not Found message to the user.

This tiny middleware addresses some of the issues. Specifically, it will change the requested location to the index you specify (default being /index.html) whenever there is a request which fulfills the following criteria:

  1. The request is a GET request
  2. which accepts text/html,
  3. is not a direct file request, i.e. the requested path does not contain a . (DOT) character and
  4. does not match a pattern provided in options.rewrites (see options below)

Usage

The middleware is available through NPM and can easily be added.

npm install --save connect-history-api-fallback

Import the library

var history = require('connect-history-api-fallback');

Now you only need to add the middleware to your application like so

var connect = require('connect');

var app = connect()
  .use(history())
  .listen(3000);

Of course you can also use this piece of middleware with express:

var express = require('express');

var app = express();
app.use(history());

Options

You can optionally pass options to the library when obtaining the middleware

var middleware = history({});

index

Override the index (default /index.html). This is the request path that will be used when the middleware identifies that the request path needs to be rewritten.

This is not the path to a file on disk. Instead it is the HTTP request path. Downstream connect/express middleware is responsible to turn this rewritten HTTP request path into actual responses, e.g. by reading a file from disk.

history({
  index: '/default.html'
});

rewrites

Override the index when the request url matches a regex pattern. You can either rewrite to a static string or use a function to transform the incoming request.

The following will rewrite a request that matches the /\/soccer/ pattern to /soccer.html.

history({
  rewrites: [
    { from: /\/soccer/, to: '/soccer.html'}
  ]
});

Alternatively functions can be used to have more control over the rewrite process. For instance, the following listing shows how requests to /libs/jquery/jquery.1.12.0.min.js and the like can be routed to ./bower_components/libs/jquery/jquery.1.12.0.min.js. You can also make use of this if you have an API version in the URL path.

history({
  rewrites: [
    {
      from: /^\/libs\/.*$/,
      to: function(context) {
        return '/bower_components' + context.parsedUrl.pathname;
      }
    }
  ]
});

The function will always be called with a context object that has the following properties:

  • parsedUrl: Information about the URL as provided by the URL module's url.parse.
  • match: An Array of matched results as provided by String.match(...).
  • request: The HTTP request object.

verbose

This middleware does not log any information by default. If you wish to activate logging, then you can do so via the verbose option or by specifying a logger function.

history({
  verbose: true
});

Alternatively use your own logger

history({
  logger: console.log.bind(console)
});

htmlAcceptHeaders

Override the default Accepts: headers that are queried when matching HTML content requests (Default: ['text/html', '*/*']).

history({
  htmlAcceptHeaders: ['text/html', 'application/xhtml+xml']
})

disableDotRule

Disables the dot rule mentioned above:

[…] is not a direct file request, i.e. the requested path does not contain a . (DOT) character […]

history({
  disableDotRule: true
})

版本与发布记录

@dsinni/connect-history-api-fallback 共发布 1 个版本,首次发布于 2021/09/28;过去 12 个月发布了 0 个版本。

版本发布日期距今
1.6.0latest2021/09/285 年前

关于 @dsinni/connect-history-api-fallback 的常见问题

@dsinni/connect-history-api-fallback 是什么?

Provides a fallback for non-existing directories so that the HTML 5 history API can be used.

如何安装 @dsinni/connect-history-api-fallback?

在项目目录运行 npm install @dsinni/connect-history-api-fallback,或使用 pnpm add / yarn add / bun add。

@dsinni/connect-history-api-fallback 支持 TypeScript 吗?

该包的 package.json 没有声明类型入口。需要类型提示时,可以在 npm 上查找社区维护的 @types/dsinni__connect-history-api-fallback 是否存在。

@dsinni/connect-history-api-fallback 支持 ESM 还是 CommonJS?

以 CommonJS 形式发布,可以直接 require();在 ES Module 项目中也可以用 import 引入(Node.js 会把 module.exports 作为默认导出)。

@dsinni/connect-history-api-fallback 需要什么版本的 Node.js?

package.json 的 engines 字段要求 Node.js >=0.8。版本不符时 npm 默认只给出警告,开启 engine-strict 后会拒绝安装。

@dsinni/connect-history-api-fallback 可以商用吗?

@dsinni/connect-history-api-fallback 使用 MIT 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

@dsinni/connect-history-api-fallback 的下载量有多少?

根据 npm 官方下载统计,@dsinni/connect-history-api-fallback 在 2026/10/13 – 2026/11/11 这 30 天内被下载 3,422 次,日均约 114 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@dsinni/connect-history-api-fallback 还在维护吗?

最新版本 1.6.0 发布于 2021/09/28(5 年前);至今共发布 1 个版本,其中过去 12 个月发布了 0 个;首次发布于 2021/09/28。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。