Minimalistic REST server inspired by express.js
Quick start
use ; // Import into scope
// Create the server
let mut server = new;
// Create a route controller clojure
let controller = ;
// Register controller on server to be triggered by a request to path / and method: GET
let path = "/";
server.get;
// Start the server on port
let PORT = 6789;
server.listen;
Route params
You can also have dynamic paths with route parameters in the path.
To specify a route parameters in the path use a colon followed by the name of the variable, e.g, /users/:userid
.
The parameter will be available on the request object with: req.route_params.get("userid")
Header variables
Header variables are also available on the request object. Tokens and other meta data are often store here.
The parameter will be available on the request object with: req.headers.get("HEADER_KEY")
Returning data
res.json("DATA_TO_RETURN")
or
res.send("DATA_TO_RETURN")
Nested routes
Nested routing is also possible. Router
objects acts as building blocks and can be composed together by calling .mount
on the parent Router with the child Router. Server extends (rust doesnt support inheritance, but Router
and Server
are implementing the same trait which is kind of similar to interfaces in other languages). So every method on Router
is also available on Server
. Server will always act as the root router from which all requests will first be directed to.
An example app that leverages nested Routers.
use ; // Import into scope
// Initialize server and routers
let mut server = new;
let mut user_router = new;
let mut post_router = new;
let getUser = ;
let createUser = ;
user_router.get; // Will be accessible at path: /users/:userId
user_router.post; // Will be accessible at path: /users
let getPost = ;
let createPost = ;
post_router.get; // Will be accessible at path: /posts/:postId
post_router.post; // Will be accessible at path: /posts
let user_router_path_prefix = "/users";
let post_router_path_prefix = "/posts";
// Mount routers into server (parent router)
server.mount;
server.mount;
// To create additional nesting just mount other routers on user_router or post_router or some other router
// Start server
let port = 6789;
server.listen;
Coming features
- Middleware