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

@xservice/server

v1.0.14

Published

Page monitor and page behavior operation architecture

Downloads

3

Readme

@xservice/server

通用的路由监听处理架构,它主要分两中模式:

  • hashchange 传统hash路由模式
  • popstate 基于html5路由模式

它能够快速为你创建路由监听,省去您的工作时间,同时保障质量和效率。

Usage

从npm下载模块:

npm i @xservice/server

在代码中使用

import Monitor, { Context } from '@fvaa/monitor';
interface CustomContext extends Context {
  // ...
}
// create instance
const createServer = Monitor<CustomContext>({
  // prefix: '/api',
  event: 'hashchange'
});

// create server 
// and listen url redirection maps on start time.
createServer(async (ctx) => console.log(ctx)).listen({
  '/': '/abc'
});

Monitor.options

  • prefix 指定当前路由前缀
  • event 您期望使用的监听事件名 hashchangepopstate 默认:hashchange
  • error 错误处理函数
  • start 进程开始处理函数
  • stop 进程结束处理函数

错误处理函数可以这样这样写

// ts type: error?(e: Error, req: Request, res: Response): void
Monitor({
  error(err, ctx) {
    console.log(err, ctx);
  },
  start(ctx) {
    console.log(ctx);
  },
  stop(err, ctx) {
    console.log(ctx);
  }
})

createServer

它用来处理请求变化时候的响应函数。它的参数即回调函数,每个回调函数必须是async函数。我们来看下它的ts定义:

export type AsyncRequestPromiseLike<T> = (req: Request, res: Response) => Promise<T>;
// ...
(...fns: Array<AsyncRequestPromiseLike<void>>) => MonitorContext

所以我们可以这样使用

createServer(
  async (req: Request, res: Response) => console.log(1, req, res),
  async (req: Request, res: Response) => console.log(2, req, res),
  async (req: Request, res: Response) => console.log(3, req, res),
  ...
)

listen

在初始化页面的时候,我们可以通过定义一个redirection map来转接路由地址:

createServer(...).listen({
  '/': '/abc',
  '/test': '/yyyyyy/123'
})

当我们当前的URL在/的时候,自动转换为/abc路由;当我们当前的URL在/test的时候,自动转换为/yyyyyy/123路由。

注意,此功能值在页面初始化时候有效。

Response

我们提供3中交互方法:

  • redirect(url:string) 为浏览器历史记录添加一条数据
  • replace(url:string) 替换当前浏览器历史记录为新记录
  • reload() 重载历史记录。
await res.redirect('/test');
await res.replace('/test');
await res.reload();

其他的虚拟请求(VPC)方法如下

await res.get('/test');
await res.post('/test', {});
await res.put('/test', {});
await res.delete('/test');

所有Request与Response的方法属性都可以在ctx上直接被调用

License

MIT

Copyright (c) 2018-present, yunjie (Evio) shen