npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

@masx200/deno-http-middleware

v3.2.1

Published

`Deno` 原生 HTTP 服务器的中间件框架

Downloads

10

Readme

deno-http-middleware

Deno 原生 HTTP 服务器的中间件框架

提供了对于nodejs环境的适配

可以使用requestListenerToMiddleWare函数把nodejs的请求处理器转换为中间件

也可以使用listener函数把中间件转换为nodejs的请求处理器

这个中间件框架受到Koa的启发

核心模块保持简洁,完全函数式编程,

允许在中间件中直接返回响应或者替换修改请求对象.

尽可能使用新的原生 API,不需要引入更多的复杂性.

对于请求的解析,使用URLSearchParams,URL,Request.prototype.json,Request.prototype.text,代替bodyParser,querystring,parseurl.

对于路由匹配,可以使用新的原生URLPattern,不使用类似于koa-Router之类的方案.

对于响应体的构建也可以使用原生Response实现大部分的响应体的编码.

返回的Response的部分属性中的body可以是任何类型,可以添加自定义中间件来把指定类型的body转换成原生Response接受的body类型.

其余常用功能在deno的标准库中有解决方案.比如说cookie,和静态文件服务.

介绍

getOriginalRequest函数

可以获得原本的请求对象

createHandler函数

根据中间件创建一个请求处理函数

接收参数middleware:中间件Middleware的数组

接受可选参数 notfoundHandler:自定义全局未找到错误处理函数。

接受可选参数 errorHandler:自定义错误处理函数

接受可选参数 responseBuilder:自定义响应构建函数

接受可选参数 retProcessor:自定义中间件返回值处理函数

中间件函数Middleware

可以是异步函数

接受参数上下文context对象和调用下一个中间件的函数next

可以返回Response或者Request或者{response:Response,request:Request}或者Response的部分属性,来修改上下文Context中的请求和响应对象,

返回值中也可以设置参数next表示中间件是否需要执行next函数.

也可以不返回,不做任何修改

返回的Response的部分属性中的body可以是任何类型,可以添加自定义中间件来把指定类型的body转换成原生Response接受的body类型.

上下文context对象

包含属性request:Request对象的部分属性,属性可修改

因为原本的Response,Request的属性都是不可修改的,所以不用原本的Response,Request对象

包含属性response:Response对象的部分属性,属性可修改

自带中间件

conditional_get:条件GET请求中间件

cors:完全跨域GET请求中间件

logger:日志中间件

json_builder:JSON响应中间件,并附带etag响应头

etag_builder:给响应体不是 stream 的提供etag响应头的中间件

method_override:覆盖请求方法的中间件,getOriginalMethod函数可以获得原本的请求方法.

stream_etag:把响应体从 stream 转换为 buffer 来计算 etag 的中间件

安装教程

  1. Deno 1.21.1

使用说明

使用 npm 安装

npm i "@masx200/deno-http-middleware"

也可以从 deno.land导入

https://deno.land/x/masx200_deno_http_middleware/mod.ts

https://deno.land/x/masx200_deno_http_middleware/middleware.ts

deno 要设定--import-map参数

--import-map https://deno.land/x/[email protected]/import_map.json

使用自带的中间件举例

import {
    conditional_get,
    cors,
    etag_builder,
    getOriginalMethod,
    json_builder,
    logger,
    method_override,
    stream_etag,
} from "https://deno.land/x/masx200_deno_http_middleware/middleware.ts";
const handler = createHandler([
    logger,
    conditional_get,
    method_override(),
    cors(),

    json_builder,
    etag_builder,
    stream_etag(),
    (ctx) => {
        const body = {
            original_Method: getOriginalMethod(ctx),
            override_method: ctx.request.method,
        };
        return { body };
    },
]);

类似nhttphello world

简单的例子,注意顺序,中间件返回response的部分属性

import { serve } from "https://deno.land/[email protected]/http/server.ts";
import { createHandler } from "https://deno.land/x/[email protected]/mod.ts";
const port = Math.floor(Math.random() * 10000 + 10000);
const handler = createHandler([
    async (ctx, next) => {
        console.log(1);
        await next();
        console.log(3);
    },
    (ctx) => {
        console.log(2);
        return { body: "hello world," + ctx.request.url };
    },
]);

const p = serve(handler, { port: port });
await p;

类似koahello world

简单的例子,注意顺序,中间件直接修改response.body

import { serve } from "https://deno.land/[email protected]/http/server.ts";
import { createHandler } from "https://deno.land/x/[email protected]/mod.ts";
const port = Math.floor(Math.random() * 10000 + 10000);
const handler = createHandler([
    async (ctx, next) => {
        console.log(1);
        await next();
        console.log(3);
    },
    (ctx) => {
        console.log(2);
        ctx.response.body = "hello world," + ctx.request.url;
        return;
    },
]);

const p = serve(handler, { port: port });
await p;

json builder 举例

自带的 json 响应构建中间件的部分代码的例子

import { isPlainObject } from "../deps.ts";
import { JSONResponse } from "../response/JSONResponse.ts";

import { Middleware, RetHandler } from "../src/Middleware.ts";

export const json_builder: Middleware = async function (
    context,
    next,
): Promise<RetHandler> {
    await next();
    const { response } = context;
    const { body } = response;

    return response instanceof Response
        ? response
        : Array.isArray(body) || isPlainObject(body)
        ? await JSONResponse(response)
        : void 0;
};

自定义错误处理程序

const h1: Middleware = async (_ctx, next) => {
    try {
        await next();
    } catch (error) {
        return new Response(error?.message, { status: 500 });
    }
};
const h2: Middleware = () => {
    throw new Error("1");
};
const composed = handler(h1, h2);

nodejs 上运行一个简单的 http 服务器

import {
    getIncomingMessage,
    json,
    listener,
} from "@masx200/deno-http-middleware";

import { createServer } from "http";

const server = createServer(
    listener(
        async (_ctx, next) => {
            try {
                await next();
            } catch (error) {
                return new Response(error?.message, { status: 500 });
            }
        },
        (ctx, next) => {
            const socket = getIncomingMessage(ctx).socket;
            const { localAddress, localPort, remoteAddress, remotePort } =
                socket;

            return json({
                localAddress,
                localPort,
                remoteAddress,
                remotePort,
                method: ctx.request.method,
                url: ctx.request.url,
                headers: Object.fromEntries(ctx.request.headers),
            });
        },
    ),
);
const port = 9000;
server.listen(port, () => console.log("http server listening port:" + port));
//server.close();