Taking advantage of the IPFS and BLESS Network at the Edge, we can serve fast web sites, apis, and static files to users all around the world.
We can do it easily and quickly using the BLESSNET CLI. In this tutorial we will dive into creating websites on BLESS Net, modifying routes, serving files, and creating a simple API.
If you recall from Getting Started tutorial, you'll recall that the BLESS Work is built with the underpinnings of WASM. Utilizing JavaScript on top.
The BLESS Network currently targets in/out style applications, that are not long running. To make this mental model easier, we've developed a framework around the architecture, that also lends itself to long running processes in the future. You should find this syntax familiar as we've modeled it after popular servers such as Express and Fastify.
Import the WebServer helper from the SDK, create a WebServer object, then start the server's run routine.
import WebServer from "@blockless/sdk-ts/dist/lib/web";
const server = new WebServer();
server.start();Using the same template as before, we'll register our first route for the root of our application /.
import WebServer from "@blockless/sdk-ts/dist/lib/web";
const server = new WebServer();
server.get("/", (req, res) => {
res.send("Hello World from Root /");
});
server.start();Adding new routes to your application is easy, add a new server.get and define the path that should be caught when queried from your application.
import WebServer from "@blockless/sdk-ts/dist/lib/web";
const server = new WebServer();
server.get("/", (req, res) => {
res.send("Hello World from Root /");
});
server.get("/other", (req, res) => {
res.send("Hello World from Root /");
});
server.start();There is often a need to server static files, images, styles, client side javascript. We've also provided a quick feature that takes care of that for you, you may recognize this command from another JavaScript based framework.
server.statics("public", "/");
The break down of this command, is that we're going to take the static folder in our project path, and we'll serve files out of it when a route that has /public in the path, and then matches the rest of the file layout.
The full project would resemble
import WebServer from "@blockless/sdk-ts/dist/lib/web";
const server = new WebServer();
server.statics("/static", "public");
server.get("/", (req, res) => {
res.send("Hello World from Root /");
});
server.get("/other", (req, res) => {
res.send("Hello World from /other");
});
server.start();