1.0.2 • Published 7 years ago

margay v1.0.2

Weekly downloads
-
License
ISC
Repository
gitlab
Last release
7 years ago

margay

Margay is a simple web framework for Node.js.

Documentation

margay

The object exported by the module.

margay.createServer()

Returns an instance of the Server class.

margay.static(filePath[, statusCode, mimeType])

Returns a simple routing function that serves a static file. If no MIME type is specified, the mime module is used to detect the file's MIME type.

Class: Response

Represents an HTTP response from the server.

response.end(chunk)

  • chunk \<Buffer> | \<string> Optional data to write to the response stream right before it is closed.

Closes the response stream.

response.header(name, value)

Sets a header of the HTTP response.

response.status(statusCode)

Sets the status code of the HTTP response.

response.write(chunk)

Writes data to the response stream. Note that the data might be manipulated by middleware if you are using any.

Class: Server

Represents the HTTP server that routes requests.

server.default(functionOrPath)

  • functionOrPath \<Function> | \<string> Routing function or path to a static file.
  • Returns: \<Server> A new server that will route unrouted requests to the specified function.

Sets the default action for when the server can't route a request. When using a string, margay.static(filePath[, statusCode[, mimeType]]) is implicitly called with a statusCode of 404.

server.listen(port)

Listens for HTTP requests on the specified port.

server.route(filter, functionOrPath)

  • filter \<Function> | \<string> Filter function or path relative to the server root.
  • functionOrPath \<Function> | \<string> Routing function or path to a static file.
  • Returns: \<Server> A new server that will route requests to the specified function.

Routes requests to the specified function. When using a string as filter, requests with a url property that matches filter will be routed. When using a string as functionOrPath, margay.static(filePath[, statusCode[, mimeType]]) is implicitly called with a statusCode of 200.

server.use(middleware)

Registers middleware that can manipulate data sent by the server.

Examples

Static file server

const margay = require("margay");
const server = margay
  .createServer()
  .route("/", "./html/index.html")
  .route("/about", "./html/about.html")
  .default("./html/404.html");

server.listen();

API server

const margay = require("margay");
const server = margay
  .createServer()
  .route("/api/users", (request, response) => {
    if (request.method === "POST") {
      // Do database operations etc.
      response
        .status(200) // "OK"
        .header("Content-Type": "application/json")
        .end(JSON.stringify({status: "success"}));
    } else {
      response
        .status(405) // "Method Not Allowed"
        .end();
    }
  })
  .default((request, response) => {
    response
      .status(404) // "Not Found"
      .end();
  });

server.listen();

License

ISC

1.0.2

7 years ago

1.0.1

7 years ago

1.0.0

7 years ago