Aqua
Aqua is a minimal and fast web framework.
Features
- Immediate parsing of the request body, query and the cookie string
- Middleware functions
- Possibility for route changes while runtime
- URL parameters
- Blazing fast
Example usage
import Aqua from "https://deno.land/x/aqua/aqua.ts";
const app = new Aqua(3100);
app.get("/", (req) => {
return "Hello, World!";
});
Routing
You can either use the short-form syntax for the GET
and POST
method
app.get("/", (req) => "Hello, World!");
app.post("/", (req) => "Hello, World!");
or use the route function
app.route("/", "GET", (req) => "Hello, World!");
Middlewares
You can register middlewares, that will be able to adjust the respond object, the following way:
app.register((req, res) => {
// Make changes to the response object
// res.content = res.content.replace("Hello", "Hi");
return res;
});
URL parameters
You can define URL parameters by using a colon followed by the key name.
app.get("/api/:action", (req) => {
return req.parameters.action;
});
Response value
You can either just return a string
app.get("/", (req) => {
return "Hello, World!";
});
or return a response object to also set cookies, headers or a status code
app.get("/", (req) => {
return {
statusCode: 200,
cookies: { hello: "I'm a cookie value" },
headers: { hello: "I'm a header value" },
content: "Hello, World!"
};
});
Cookies and headers are just getting appended, so no information is getting lost by providing custom ones. However, you can still overwrite existing headers.
Benchmarks
Framework | Version | Avg RPS | Router? |
---|---|---|---|
Deno HTTP | 0.61.0 | 22614 | No |
Aqua | 1.0.2 | 21360 | Yes |
Fastro | 0.13.5 | 21292 | Yes |
Drash | 1.0.7 | 18020 | Yes |
Denotrain | 0.5.2 | 13430 | Yes |
Attain | master | 9597 | Yes |
More examples
Respond with the content of a file
app.get("/", async (req) => {
return await app.render("index.html");
});
Please note that you must run your application with the --allow-read
flag.
Provide own fallback handler
Your provided fallback handler will be executed if no route has been found.
app.provideFallback((req) => {
return "No page found, sorry!";
});
Redirect a request
app.get("/dashboard", (req) => {
return { redirect: "/login" };
});
Regex paths
You can provide a RegExp object instead of a string and receive the matches.
app.get(new RegExp("\/(.*)"), (req) => {
console.log(req.matches); // GET /hello-world -> [ "hello-world" ]
return "Hello, World!";
});
TLS
You can enable TLS the following way:
const app = new Aqua(3001, {
tls: {
hostname: "localhost",
certFile: "localhost.crt",
keyFile: "localhost.key"
}
});
The example above would handle requests coming to https://localhost:3001
.
Handle HTTP and HTTPS requests
You are able to provide the TLS certificate to a different port and let the default port still handle HTTP requests.
const app = new Aqua(3001, {
tls: {
hostname: "localhost",
certFile: "localhost.crt",
keyFile: "localhost.key",
independentPort: 3002
}
});
The example above would allow you to handle requests to http://localhost:3001
and https://localhost:3002
at the same time.