2.0.6 • Published 2 years ago
micro-spark v2.0.6
Micro-spark
A tiny but complete http server framework without any external dependencies.
Get started
const Spark = require("micro-spark");
new Spark()
.serve("/assets/*")
.declare("db", {
// register a plugin to "ctx"
query: (user) => {}
})
.before(ctx => {
// dosomething before route executed
})
.after(ctx => {
// dosomething after route executed
})
.get("/:name", ctx => {
return ctx.params.name;
})
.get("/tmpl", ctx => {
return ctx.render("<div>hello, {{= name}}</div>", { name: "world" });
})
.get("/tmplfile", ctx => {
return ctx.view("tmpl.html", { name: "world" });
})
.listen();
API Reference
Constructor
app.declare(name, dest)
app.serve(path)
app.before(function)
app.after(function)
app.all(path, handle)
app.get(path, handle)
app.post(path, handle)
- ...
Context
Properties
ctx.params
Get params in route pathctx.query
Get params in query stringctx.method
Get request methodctx.path
Get request pathctx.url
Get request full hrefctx.protocol
Get request protocolctx.host
Get request hostctx.hostname
Get request hostnamectx.origin
Get request originctx.headers
Get headers objectctx.cookies
Get cookies objectctx.status
Get response status codectx.status=
Set response status codectx.request
The native request objectctx.response
The native response object
Methods
ctx.get(name)
Get request headers by namectx.set(name, value)
Set response headersctx.cookie(name, value[, options])
Set cookiesasync ctx.json()
Get request body in jsonasync ctx.text()
Get request body in textasync ctx.buffer()
Get request body in bufferctx.send(body[, status])
Send response to client with status default 200ctx.redirect(url[, status])
Redirect url with status default 301ctx.view(path, data)
Render template with a filectx.throw(message, status)
Throw an error with status code
Template Syntax
{{> file }}
Include partial file{{# name }} {{# }}
Define block with name{{## name }}
Use block with name{{ evaluate }}
{{= interpolate }}
{{? conditional }} {{?? }} {{? }}
{{~ iterate:value:index }} {{~ }}
2.0.3
2 years ago
2.0.2
2 years ago
2.0.5
2 years ago
2.0.4
2 years ago
2.0.6
2 years ago
2.0.1
2 years ago
2.0.0
2 years ago
1.2.0
3 years ago
1.1.11
3 years ago
1.1.10
3 years ago
1.1.9
4 years ago
1.1.8
4 years ago
1.1.7
4 years ago
1.1.6
4 years ago
1.1.5
4 years ago
1.1.4
4 years ago
1.1.3
4 years ago
1.1.1
4 years ago
1.1.0
4 years ago
1.1.2
4 years ago
1.0.16
4 years ago
1.0.15
4 years ago
1.0.9
4 years ago
1.0.8
4 years ago
1.0.7
4 years ago
1.0.6
4 years ago
1.0.5
4 years ago
1.0.4
4 years ago
1.0.11
4 years ago
1.0.10
4 years ago
1.0.14
4 years ago
1.0.13
4 years ago
1.0.12
4 years ago
1.0.3
4 years ago
1.0.2
4 years ago
1.0.1
4 years ago
1.0.0
4 years ago