Elegant powerful nodejs server for nanoservice use cases, written in tiny TypeScript module.
import {type AlwatrConnection, AlwatrNanoServer} from 'https://esm.run/@alwatr/nano-server';
const nanoServer = new AlwatrNanoServer();
nanoServer.route('GET', '/', async (connection: AlwatrConnection) => {
connection.reply({
ok: true,
data: {
app: 'Alwatr Nanoservice Starter Kit',
message: 'Hello ;)',
},
});
});
Create a server for nanoservice use cases.
Example:
import {AlwatrNanoServer} from 'https://esm.run/@alwatr/nano-server';
const nanoServer = new AlwatrNanoServer();
Stops the HTTP server from accepting new connections.
Refers to how an application’s endpoints (URIs) respond to client requests.
Example:
nanoServer.route('GET', '/', middleware);
????
async function middleware(connection: AlwatrConnection) => {
connection.reply({
ok: true,
data: {
app: 'Alwatr Nanoservice Starter Kit',
message: 'Hello ;)',
},
});
});
Request URL.
`connection.method: "ALL" | "GET" | "HEAD" | "POST" | "PUT" | "DELETE" | "CONNECT" | "TRACE" | "OPTIONS" | "PATCH"
`
Request method.
Get request body for POST, PUT and POST methods..
Example:
const body = await connection.getBody();
Get the token placed in the request header.
Example:
nanoServer.route('GET', '/', async (connection) => {
connection.reply({
ok: true,
data: {
app: 'Alwatr Nanoservice Starter Kit',
message: 'Hello ;)',
},
});
});
Parse request body.
Example:
const bodyData = await connection.requireJsonBody();
if (bodyData == null) return;
Parse and validate request token. Returns request token.
Example:
const token = connection.requireToken((token) => token.length > 12);
if (token == null) return;
Parse and validate query params. Returns query params object.
Example:
const params = connection.requireQueryParams<{id: string}>({id: 'string'});
if (params == null) return;
console.log(params.id);