v1.0.1
π Light-weight http server
Repository
Current version released
3 years ago
π Light-weight http server
This is a small HTTP server library written with π and TypeScript and is meant to be simple and lightweight.
Server
import { Server } from 'mod.ts';
const server = new Server();
server.on('/', 'GET')(({ response }) => {
response.body = 'Hello World!';
});
server.listen(8080);
Middleware
import { Server } from 'mod.ts';
const server = new Server();
server.use(() => {
console.log(Date.now(), 'Request');
});
server.on('/*')(({ respond }) => {
respond('Hello, HTTP!');
});
server.listen(8080);
Routers
import { Server, Router } from 'mod.ts';
const server = new Server();
const users = new Router('/users');
users.on('/:id', 'GET')(({ request, respond }) => {
respond(localStorage.getItem(request.url.params.id));
});
users.on('/', 'POST')(async ({ request, respond }) => {
const user = await request.body.json();
localStorage.setItem([user.id], user);
respond(null, { status: 202 });
});
server.use(users);
server.listen(8080);
Static Files
import { Server, files } from 'mod.ts';
const server = new Server();
server.use(files('./assets/'));
server.listen(8080);
WebSockets
import { Server, upgrade } from 'mod.ts';
const server = new Server();
server.on('/ws', 'GET')(async (data) => {
const socket = upgrade(request);
if (!socket) return;
socket.addEventListener('open', () => socket.send('Hello, Socket!'));
});