1.1.6 • Published 4 years ago

sofer v1.1.6

Weekly downloads
3
License
ISC
Repository
github
Last release
4 years ago

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.
AttributesIntroduction
context.urlGet the requested route
context.methodget the request method, usually GET, or POST
context.typeCan set the encoding method of response
context.queryGet the parameters in the route
context.paramsGet the parameters of the dynamic route
context.statusCan set the response status code
context.header Or context.headersGet the request header
context.cookieGet the cookie carried by the request header
context.charsetGet the encoding method, the default is utf8
There are some commonly used function properties
functionIntroduction
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)
1.1.6

4 years ago

1.1.5

5 years ago

1.1.4

5 years ago

1.1.3

5 years ago

1.1.2

5 years ago

1.1.1

5 years ago

1.1.0

5 years ago

1.0.9

5 years ago

1.0.8

5 years ago

1.0.7

5 years ago

1.0.6

5 years ago

1.0.5

5 years ago

1.0.4

5 years ago

1.0.3

5 years ago

1.0.2

5 years ago

1.0.1

5 years ago

1.0.0

5 years ago