v2.0.3
π 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.
β οΈ The library is still in development and may change a lot.
Server
import { Server } from 'mod.ts';
const server = new Server();
server.on('/', 'GET')(({ respond }) => {
respond({ body: 'Hello World!' });
});
server.listen(8080);
Middleware
import { Server } from 'mod.ts';
const server = new Server();
server.use('/*', 'GET')(() => {
console.log('A new GET request');
});
server.on('/*')(({ respond }) => {
respond({ body: 'Hello World!' });
});
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({ body: localStorage.getItem(request.url.params.id) });
});
users.on('/', 'POST')(async ({ request, respond }) => {
const user = await request.body.json();
localStorage.setItem([user.id], user);
respond({ status: 200 });
});
server.use()(users);
server.listen(8080);
Static Files
import { Server } from 'mod.ts';
const server = new Server();
server.static('/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!'));
});