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 🙏

© 2025 – Pkg Stats / Ryan Hefner

@imhjh/server

v1.2.1

Published

简易 node 服务器

Downloads

6

Readme

@imhjh/server 文档

源码

一个简易的 node web 框架。

安装

# npm
npm install @imhjh/server --save-dev
# yarn
yarn add @imhjh/server --save

使用

createApp实例通过中间件的形式实现请求解析和传参.

import { createApp } from "@imhjh/server"

createApp()
  // 最后执行的中间件, 结束请求, 返回数据
  .use(({ response }) => {
    response.end()
  })
  // 开启设置监听
  .listen({ port: 8080 }, (server) => {
    console.log("启动成功")
  })

API

createApp

创建服务器

createApp(options: AppOptions): AppObject

AppOptions

declare interface RequestResult {
  statusCode?: number
  data: any
}
declare interface RouteHandler {
  // MiddlewareOptions: 经过中间件处理后的配置
  (options: MiddlewareOptions): RequestResult
}
declare interface RouteObject {
  path: string /** 本级路径片段 */
  handler: RouteHandler /** 对应路径的请求方法 */
  children?: RouteObject[] /** 子级路径列表 */
}
const routes: RouteObject[] = [
  {
    path: "/test",
    handler: () => ({ statusCode: 200, data: "" })
  }
]

declare interface AppOptions {
  routes?: RouteObject[] /** 路由配置 */
  strict?: boolean /** 是否启用路由严格配置,匹配 "/" 结尾 */
  serveOptions?: ServeOptions /** http.createServer 配置 */
  readDirname?: string /** 用于读取文件的目录 */
}
const options: AppOptions = {
  routes: routes,
  strict: false,
  serveOptions: {},
  readDirname: ""
}

AppObject

declare interface RequestLocation {
  /** 完整请求地址
   * @example http://127.0.0.1:80/path?query=params#hash */
  href: string
  /** 协议+主机+端口
   * @example http://127.0.0.1:80 */
  origin: string
  /** 协议
   * @example http */
  protocol: string
  /** 账号 */
  username: string
  /** 密码 */
  password: string
  /** 主机+端口
   * @example 127.0.0.1:80 */
  host: string
  /** 主机
   * @example 127.0.0.1 */
  hostname: string
  /** 端口
   * @example 80 */
  port: string
  /** 请求路径
   * @example /path */
  pathname: string
  /** 参数
   * @example ?query=params */
  search: string
  /** URLSearchParams对象 */
  searchParams: URLSearchParams
  /** hash参数
   * @example #hash */
  hash: string
}
declare interface RequestQuery {
  method?: string
  extname?: string
  query?: { [k: string]: any } // url携带参数
  postData?: { [k: string]: any } // 非get请求参数
}
// 通过内置中间件解析出来的配置
declare interface ExtraMiddlewareOptions {
  server: Server /** 服务器实例 */
  router: RouterObject /** 使用 defineRouter 解析出来的路由键值对 */
  strict: boolean /** 接口地址是否严格匹配 */
  requestLocation?: RequestLocation /** 请求地址解析 */
  requestQuery?: RequestQuery /** 请求参数解析 */
  requestResult?: RequestResult /** 请求返回结果 */
  readDirname?: string /** 用于读取文件的目录 */
}
declare interface MiddlewareOptions extends ExtraMiddlewareOptions {
  request: IncomingMessage /** request 对象 */
  response: ServerResponse /** response 对象 */
  [k: string]: any
}
declare interface MiddlewareFunction {
  (options: MiddlewareOptions, next: () => void): void
}
declare interface AppObject {
  server: Server /** http.createServer 服务器对象 */
  use: (middleware: MiddlewareFunction) => AppObject
  listen: (options: ListenOptions, listeningListener: (server: Server) => void) => void
}

const app: AppObject = createApp()

app
  .use((options, next) => {
    next() // 触发下一个中间件处理
  })
  .listen({ port: 8080 }, (server) => {
    // 启动监听后的回调
  })

getDirname

用于 esm 环境获取 dirname

// 通过 import.meta 获取当前目录的绝对地址
const dirname = getDirname(import.meta)

useAddress

解析 server 获取监听的 http

const app = createApp()

app.listen({ port: 8080 }, (server) => {
  const address = useAddress(server)
  // address.port: 8080
  // address.family: IPV4 | IPV6
  // address.hostname: 127.0.0.1
})

readFile

同步读取文件

const data = readFile(dirname, filename) // 内部通过 path.join 合并地址

getMIME

根据后缀获取对应的媒体类型

getMIME("html") // "text/html"