margay v1.0.2
margay
Margay is a simple web framework for Node.js.
Documentation
margay
The object exported by the module.
margay.createServer()
- Returns: \<Server>
Returns an instance of the Server class.
margay.static(filePath[, statusCode, mimeType])
filePath
\<string> Relative path to the file.statusCode
\<number> Status code for the HTTP response. Default:200
mimeType
\<string> MIME type of the file.- Returns: \<Function>
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)
middleware
\<Function>- Returns: \<Server> A new server that will use the specified 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();