1.1.5 • Published 1 year ago

node-cross v1.1.5

Weekly downloads
-
License
MIT
Repository
github
Last release
1 year ago

node-cross

Node-cross is a tiny but complete http server framework for nodejs. The features include:

  • Application context
  • Middlewares like Koa.js
  • Built-in static service
  • Built-in routing service
  • Built-in template engine

Installation

npm i node-cross

Get Started

const Cross = require("node-cross");

new Cross()
  .static("/assets")
  .engine({
    root: "template",
    imports: { globalName: "cross" }
  })
  .on("error", (err, ctx) => {
    console.error(err);
    ctx.body = {
      status: ctx.status,
      message: ctx.body
    };
  })
  .use(async (ctx, next) => {
    ctx.custom = "xxxx";
    await next();
  })
  .get("/", async ctx => {
    console.log(ctx.query);
    console.log(ctx.params)
  })
  .listen();

API Reference

Methods

  • app.engine(options) Enable template engine with options { root, imports }.
  • app.static(path) Serve static resources with the given path.
  • app.on("error", function) Custom unified error handling.
  • app.use(function) Add a middleware like koa.js.
  • app.get(path, [tmpl,] function) Add dynamic route including post, put, delete and other standard request methods, it will auto-render template if tmpl parameter exists.
  • app.listen([port]) Create and start an application server on the specified port.
  • app.callback() Return a request handler for node's native http server.

Context

Properties

  • ctx.params Get params in route path, wildcard supports
  • 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.body Get response body
  • ctx.request Get native request
  • ctx.response Get native response
  • ctx.status= Set response status code
  • ctx.body= Set response body

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.redirect(url[, status]) Redirect url with status default 301
  • ctx.view(path, data) Render template file, only if engine enabled.
  • ctx.render(path, data) Render template text, only if engine enabled.
  • ctx.throw(message, status) Throw an error with status code

Route Syntax

  • /static static route
  • /* Wildcard route, it will return wildcard variable in ctx.params
  • /:user
  • /:user?
  • /:user(\\d+)

Template Syntax

See https://github.com/metadream/tmplet.js

1.1.1

1 year ago

1.1.0

1 year ago

1.0.9

1 year ago

1.0.8

1 year ago

1.0.7

1 year ago

1.1.5

1 year ago

1.0.6

1 year ago

1.1.3

1 year ago

1.1.2

1 year ago

1.0.5

2 years ago

1.0.4

2 years ago

1.0.3

2 years ago

1.0.2

2 years ago

1.0.1

2 years ago

1.0.0

2 years ago