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

cuka

v1.0.11

Published

Cuka web app framework

Downloads

1

Readme

Cuka

一个基于 NodeJS 的轻量级且无任何其他依赖的 HTTP 服务开发框架。同时支持 cjs 和 esm 两种模块化规范。

框架特性:

  1. 支持自定义中间件函数,支持对请求进行预处理。
  2. 内置错误处理,保证服务稳定运行不崩溃,安全运行一百年。
  3. 路由、方法、处理函数 三段式设计,丝滑的开发体验。

安装

npm install cuka

快速上手

import Cuka from "cuka";

const app = new Cuka();

app.listen(3000, () => {
  console.log("Server is listening on http://127.0.0.1:3000");
});

app.on("/", "get", (ctx) => {
  ctx.end("Hello, World!");
});

设置响应头部

  1. 设置全局响应头部
import Cuka from "cuka";

const app = new Cuka();

app.listen(3000, () => {
  console.log("Server is listening on http://127.0.0.1:3000");
});

// 设置全局响应头部,对所有请求生效
app.setHeader("Content-Type", "text/plain;charset=utf-8");

app.on("/", "get", (ctx) => {
  ctx.end("Hello, World!");
});
  1. 设置局部响应头部
import Cuka from "cuka";

const app = new Cuka();

app.listen(3000, () => {
  console.log("Server is listening on http://127.0.0.1:3000");
});

app.on("/", "get", (ctx) => {
  // 设置局部响应头部,只对当前请求生效
  ctx.setHeader("Content-Type", "text/plain;charset=utf-8");
  ctx.end("Hello, World!");
});

使用中间件

中间件函数永远在请求到达路由方法处理函数之前调用。

import Cuka from "cuka";

const app = new Cuka();

app.listen(3000, () => {
  console.log("Server is listening on http://127.0.0.1:3000");
});

// 使用中间件:打印每个请求的信息:路由、方法
app.use((ctx) => {
  const { route, method } = ctx.request;
  console.log(`${route} --- ${method}`);
});

app.on("/", "get", (ctx) => {
  ctx.end("Hello, World!");
});

参数解析

  1. 解析query参数

场景:假设服务端收到一个get请求,请求的路由为:/user?name=张三

import Cuka from "cuka";

const app = new Cuka();

app.listen(3000, () => {
  console.log("Server is listening on http://127.0.0.1:3000");
});

app.on("/user", "get", (ctx) => {
  console.log(ctx.request.query); // 这里将打印:{ name: "张三" }
});
  1. 解析body参数

场景:假设服务端收到一个post请求,请求的路由为:/user, 并且请求体携带了JSON数据 { name: "张三" }

import Cuka from "cuka";

const app = new Cuka();

app.listen(3000, () => {
  console.log("Server is listening on http://127.0.0.1:3000");
});

app.on("/user", "post", (ctx) => {
  console.log(ctx.request.body); // 这里将打印:{ name: "张三" }
});