Puddle Framework is a web framework that runs on Deno.
The goal of this framework is to make it easy for even novice programmers to set up a web server.
Shell (Mac, Linux):
curl -fsSL https://deno.land/x/install/install.sh | shPowerShell (Windows):
iwr https://deno.land/x/install/install.ps1 -useb | iexHomebrew (Mac):
brew install denoSee deno.land for more installation options.
You can start the server with the command "deno run -A . /server.ts".
import { System, Config } from "https://github.com/PuddleServer/Puddle/raw/v1.1.2-beta/mod.ts";
System.listen(8080, (conf: Config) => {
console.log(`The server running on http://${conf.hostname}:${conf.port}`);
});You can host a static server by simply specifying the file path!
import { System, Config } from "https://github.com/PuddleServer/Puddle/raw/v1.1.2-beta/mod.ts";
System.createRoute("./index.html").URL("/", "/Top");
System.createRoutes("./styles/*");
System.listen(8080, (conf: Config) => {
console.log(`The server running on http://${conf.hostname}:${conf.port}`);
});If you want to set up dynamic processing, connect the method to the route you created and specify the handler function.
import { System, Config, SystemRequest, SystemResponse } from "https://github.com/PuddleServer/Puddle/raw/v1.1.2-beta/mod.ts";
System.createRoute("ContactForm").URL("/Contact")
.GET(async (req: SystemRequest, res: SystemResponse) => {
await res.setFile("./contact_form.html");
})
.POST(async (req: SystemRequest, res: SystemResponse) => {
const body: string = await req.readBody();
if(!body.length) return {
status: 400,
headers: new Headers(),
body: "No message."
};
// Process body data.
res.setText("Collect.");
});
System.listen(8080, (conf: Config) => {
console.log(`The server running on http://${conf.hostname}:${conf.port}`);
});The Puddle Framework provides two ways to redirect clients.
import { System, Config, SystemRequest, SystemResponse, redirect } from "https://github.com/PuddleServer/Puddle/raw/v1.1.2-beta/mod.ts";
System.createRoute("example1").URL("/Redirect1")
.GET(redirect("https://www.example.com"));
System.createRoute("example2").URL("/Redirect2")
.GET((req: SystemRequest, res: SystemResponse) => {
res.redirect("https://www.example.com");
});
System.listen(8080, (conf: Config) => {
console.log(`The server running on http://${conf.hostname}:${conf.port}`);
});Websockets server routing can be done in the same way as web server routing!
import { System, Config, SystemRequest, WebSocketClient } from "https://github.com/PuddleServer/Puddle/raw/v1.1.2-beta/mod.ts";
System.createRoute("./webSocket.html").URL("/", "/top");
System.createRoute("/ws").WebSocket();
System.listen(8080, (conf: Config)=>{
console.log(`The server running on http://${conf.hostname}:${conf.port}`);
});To handle each Websocket event, set up a handler function by connecting a method to the created Websocket route.
import { System, Config, SystemRequest, WebSocketClient } from "https://github.com/PuddleServer/Puddle/raw/v1.1.2-beta/mod.ts";
System.createRoute("./webSocket.html").URL("/", "/top");
System.createRoute("/ws").WebSocket()
.onopen((client: WebSocketClient) => {
client.reply("Connection to server complete.");
})
.onmessage((client: WebSocketClient) => {
client.sendAll(client.message);
});
System.listen(8080, (conf: Config)=>{
console.log(`The server running on http://${conf.hostname}:${conf.port}`);
});Digest access authentication is one of the agreed-upon methods a web server can use to negotiate credentials, such as username or password, with a user's web browser.
System.createRoute("/auth.html").URL("/authPage")
.AUTH("userName", "password");You can use OAuth 2.0 with the Google API to retrieve user information.
const client_id = "*******.apps.googleusercontent.com";
const client_secret = "*******";
const redirect_url = "http://localhost:8080/google_auth2";
System.AUTH.GOOGLE(client_id, client_secret, redirect_url).URL("/Login")
.LOGIN((
req: SystemRequest,
res: SystemResponse,
user_info: { [key:string]: string; }
)=>{
// "user_info" is the user information received from Google API.
console.log(user_info);
res.setText(JSON.stringify(user_info));
});It provides functions to easily manipulate data in JSON files!
import { PuddleJSON } from "https://github.com/PuddleServer/Puddle/raw/v1.1.2-beta/mod.ts";
const USERS = PuddleJSON.USE("./users.json", {
id: ["UNIQUE", "NOT NULL", "AUTO INCREMENT"],
name: ["NOT NULL"],
age: []
});
USERS.INSERT({ name: "Steve", age: 20 });
USERS.SELECT({ id: 1 }).RESULT("name", "age");
USERS.SELECTIF( row => ({ age: Number(row.age) >= 18 }) ).RESULT();
USERS.SELECT({ id: 1 }, 1 ).UPDATE({ age: 21 });
USERS.SELECT({ age: null }).DELETE();