Skip to content

musakui/saaba

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

7 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

saaba

a smol node server utility

npm

Warning this is still experimental and APIs are expected to change a lot

Usage

import { createServer } from 'node:http'
import {
	createListener,
	getJSON,
	NotFoundError,
	NotAllowedError,
} from '@musakui/saaba'

const listener = createListener({
	cors: true,
	emptyFavicon: true,
	handleRequest: async (ctx) => {
		if (ctx.path === '/') {
			if (ctx.method === 'GET') return { status: 'ok' }

			if (ctx.method === 'POST') {
				const data = await getJSON(ctx)
				return { status: 'ok', data }
			}

			throw new NotAllowedError()
		}

		if (ctx.path === '/demo') {
			if (ctx.method !== 'GET') throw new NotAllowedError()

			// query as URLSearchParams
			const name = ctx.query.get('name') || 'no name'

			// manually set header
			ctx.res.setHeader('content-type', 'text/html')

			return `<html><title>demo</title><body>hi, ${name}</body></html>`
		}

		throw new NotFoundError()
	},
})

createServer(8000, listener).then(() => console.log('server started'))

About

鯖 - micro server

Resources

License

Code of conduct

Security policy

Stars

Watchers

Forks