2.0.6 • Published 2 years ago

micro-spark v2.0.6

Weekly downloads
2
License
MIT
Repository
github
Last release
2 years ago

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 path
  • ctx.query Get params in query string
  • ctx.method Get request method
  • ctx.path Get request path
  • ctx.url Get request full href
  • ctx.protocol Get request protocol
  • ctx.host Get request host
  • ctx.hostname Get request hostname
  • ctx.origin Get request origin
  • ctx.headers Get headers object
  • ctx.cookies Get cookies object
  • ctx.status Get response status code
  • ctx.status= Set response status code
  • ctx.request The native request object
  • ctx.response The native response object

Methods

  • ctx.get(name) Get request headers by name
  • ctx.set(name, value) Set response headers
  • ctx.cookie(name, value[, options]) Set cookies
  • async ctx.json() Get request body in json
  • async ctx.text() Get request body in text
  • async ctx.buffer() Get request body in buffer
  • ctx.send(body[, status]) Send response to client with status default 200
  • ctx.redirect(url[, status]) Redirect url with status default 301
  • ctx.view(path, data) Render template with a file
  • ctx.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