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

sofer

v1.1.6

Published

A framework similar to koa, able to quickly develop web servers

Downloads

6

Readme

A framework sofer that can quickly develop web servers

download

npm i sofer

Instructions
// server.js
const sofer = require("sofer")
const server = new sofer()
server.get("/index",function(ctx){
	ctx.body = "Hello World"
})
server.listen(3000)
testing

在这里插入图片描述

Request with parameters
server.get("/index",function(ctx){
	// ctx.query can parse routing parameterscan
	ctx.body = ctx.query
})

在这里插入图片描述

Dynamic routing
server.get("/index/:name",function(ctx){
	// ctx.params can parse Dynamic routing
	ctx.body = ctx.params
})

在这里插入图片描述

In the design of the registered route, I try to keep consistent with the koa framework.

context

This is a very important concept, this is the encapsulation of request and response, context.req is request, context.res is response, in addition, some common properties are reset, see the following table.

|Attributes|Introduction| |------|------ |context.url|Get the requested route |context.method|get the request method, usually GET, or POST |context.type|Can set the encoding method of response |context.query|Get the parameters in the route |context.params|Get the parameters of the dynamic route |context.status|Can set the response status code |context.header Or context.headers|Get the request header |context.cookie|Get the cookie carried by the request header |context.charset|Get the encoding method, the default is utf8

There are some commonly used function properties

|function|Introduction| |------|------ |context.getHeader(name)|Get the attribute specified by the request header |context.setHeader(name,value)|Set the specified response header |context.accept(name)|Get the attribute at the beginning of the specified accpet- in the request header |context.setCookie(name,value)|Set the cookie carried in the response header |context.getCookie(name)|Get the specified cookie carried in the request header |context.read(file-path)|Returns the specified file, file-path is the path to the file |context.media(path,name,cb)|Process data from media files and set where files are stored |context.mediaReader(path,name,cb)|Parsing information about media files and setting where files are stored

Use of middleware

// Registration middleware
server.use(middleware)
  • Currently I just have two middleware built in, postparse and sofer-views.
  • postparse is used to retrieve and parse post request data. Sofer-views is used to manage static resources. In fact, you can also write middleware, just use use() to register middleware.
// If you need to use it, you can use the built-in middleware directly, or you can write middleware yourself.
server.use(sofer.postparse()).use(sofer.views())

Module separation

  • Inside the sofer is a built-in Router for module separation, and the route() function is used to register separate modules like this:
// home.js
const sofer = require("sofer")
const Router = sofer.Router
const router = new Router()
router.get("/index",function(ctx){
	ctx.body = "this is a home index..."
})
module.exports = router
// server.js
const sofer = require("sofer")
// Introducing the home module
const home = require("./home.js")
const server = new sofer()
server.get("/index",function(ctx){
	ctx.body = "Hello World"
})
// Registering a module using the route() function
server.route("/home",home)
server.listen(3000)