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 🙏

© 2024 – Pkg Stats / Ryan Hefner

@xie-work/common-http

v0.3.2

Published

## 安装与使用

Downloads

211

Readme

@xie-work/common-http

安装与使用

1. 安装

在前端项目中使用 npm 或者 yarn 命令进行安装

# use npm
npm install @xie-work/common-http
# use yarn
yarn add @xie-work/common-http

2. 使用

import {server} from '@xie-work/common-http'
import * as https from "node:https";

const httpServer = server.listen(3000)

// 匹配所有的请求
httpServer.route("hello world")

// 精准匹配/api
httpServer.route("/api", "hello world")

// 匹配/api/下的所有请求
httpServer.route("/api/*", "hello world")

// 支持方法匹配
httpServer.route("POST", "/api", "hello world")
httpServer.route("*", "/api", "hello world")

// 支持函数返回
httpServer.route("/api", () => "hello world") // text/plain
httpServer.route("/api", () => ({a: 1})) // application/json

// 支持自处理
httpServer.route("/api", (res, req) => {
    res.writeHead(200, {'Content-Type': 'text/plain'});
    res.write('hello world');
})

// 支持异步处理
httpServer.route("/api", async (res, req) => {
    for (let i = 0; i < 10; i++) {
        res.write('hello world\n');
        await new Promise(resolve => setTimeout(resolve, 1000));
    }
})
  • 精准匹配的优先级高于通配符匹配
  • 通配符之间的优先级是按照定义的顺序,后定义的优先级高
  • 支持函数返回,函数返回的数据会根据返回的数据类型自动设置Content-Type
  • 函数处理的过程中如果通过res.write写入了数据,则上面的规则会失效
  • 不建议在函数处理的过程中使用res.end,因为res.end会关闭连接,函数的返回值会被忽略

处理函数的参数说明

  • res: 与nodejs的http模块的response对象一致
  • req: 与nodejs的http模块的request对象一致,并且扩展了多个属性:
    • uri: 请求的uri
    • method: 请求的方法
    • headers: 请求的头部信息的对象
    • remoteAddress: 请求的远程地址
    • remotePort: 请求的远程端口
    • body: post请求的body,
    • searchParams: 请求的url中的search字符串转换成的URLSearchParams对象
    • params: 自动根据请求类型将body或者searchParams转换成的对象