0.2.3 • Published 6 years ago

leafless v0.2.3

Weekly downloads
2
License
Apache-2.0
Repository
github
Last release
6 years ago

Leafless

Leafless is a tiny async-await http library for NodeJS

Installation

$ yarn add leafless

Usage: Hello World

let LL = require("leafless");
let app = new LL();

// hello world [GET]
app.route("/hello", {
  async get(ctx) {
    ctx.response.text("Hello World");
  }
})

/** a POST request to /hello returns a 405 */

// allow both POST and GET requests
app.route("/hello2", {
  async get(ctx) {
    ctx.response.text("Hello World");
  }

  // ContentType application/json
  async post(ctx) {
    ctx.response.json({ data:"Hello World"});
  }
})

// read http request body
app.route("/data", {
  async post(ctx) {
    // read request body as json
    let body = await ctx.request.json();
    if (!body.code) {
      ctx.response.status(400);
      ctx.response.json({error: "body must have code!"});
      return;
    }
    ctx.response.json({data: "everything is cool"});
  }
})

// serve a file from the filesystem as response
app.route("/data", {
  async post(ctx) {
    ctx.response.sendFile("/absolute/path/to/file");
  }
})

app.listen(8000); // listen and serve
0.2.3

6 years ago

0.2.2

6 years ago

0.2.1

7 years ago

0.2.0

7 years ago

0.1.2

7 years ago

0.1.1

7 years ago

0.1.0

7 years ago

0.0.10

7 years ago

0.0.9

7 years ago

0.0.8

7 years ago

0.0.7

7 years ago

0.0.6

7 years ago

0.0.5

7 years ago

0.0.4

7 years ago

0.0.3

7 years ago

0.0.2

8 years ago

0.0.1

8 years ago

0.0.0

8 years ago