Redirects (middleware)
用于为某些 URL 返回重定向的中间件。
配置参数
- redirects record
A map of redirects
- strict boolean
Whether distinguish the trailing slash or not
描述
当请求 URL 包含在提供的 URL 列表中时,此中间件会返回重定向。它与 重定向插件 的 JSON 输出方法兼容。
安装
此中间件必须与 Lume 的 HTTP 服务器 一起使用。要在生产环境中使用它,你需要一个运行 Deno 服务器的主机,例如 Deno Deploy。
创建一个入口点文件(例如,serve.ts
),包含以下代码:
import Server from "lume/core/server.ts";
import redirects from "lume/middlewares/redirects.ts";
const server = new Server();
server.use(redirects({
redirects: {
"/from/": "/to/",
"/from2/": "/to2/",
// Use an object to configure the status code. (301 by default)
"/from3/": {
to: "/to2/",
code: 302,
},
},
strict: false, // configure whether distinguish the trailing slash or not (true by default)
}));
server.start();
本地开发
你可以配置 Lume 的开发服务器,在 _config.ts
文件中使用此中间件。
import lume from "lume/mod.ts";
import redirects from "lume/middlewares/redirects.ts";
import myRedirections from "./my-redirections.json" with { type: "json" };
const site = lume({
server: {
middlewares: [redirects({ redirects: myRedirections })],
},
});
export default site;